View Source TypeResolver (TypeResolver v0.1.6)

Type resolver is a library to resolve spec-types at compile time. That is, reducing the specs to only native types by resolving user-defined, built-in and remote types. The result is a type expressed in easy to handle structs of types.

Rational

Often times we want to analyse spec-types in macros and derive functionality from these types. Analysing and working with AST from Code.Typespec or Erlang AST is challenging and cumbersome. Furthermore, there is no standalone library that resolves user-defined types, built-in types and remote-types. This library fills this gap.

Usage

The library offers the macro TypeResolver.resolve/1 that can be used as follows:

resolve(integer() | String.t())

It returns the following representation:

%TypeResolver.UnionT{
  inner: [%TypeResolver.Types.IntegerT{}, %TypeResolver.Types.BinaryT{}]
}

The library is also capable of resolving type parameters.

For a complete list of result-types, see TypeResolver.Types.

Type exporter

To resolve remote types, we rely on Code.Typespec.fetch_types/1. For this function to work, the module has to be compiled into a beam file, which cannot always be ensured at macro expansion time.

To handle this problem, we provide the module TypeResolver.TypeExporter. This module takes care of types being exported and makes them available at macro expansion time. The module has to be used in the remote module that contains the type, as follows:

defmodule MyRemoteModule do 
  use TypeResolver.TypeExporter

  @type my_remote_t :: ...
end

Note, that this is not a problem for library code since for dependencies, all files have been compiled and written to beam-files before the compilation of our own code.

Limitations

  • Currently this library cannot handle recursive types and does not detect them. When using recursive times, compilation will be stuck in an infinite recursion.
  • Struct types don't get their member resolved
  • doesn't work with opaque types

Summary

Functions

This marco takes a spec expression and returns a struct-based representation of the types, resolved to only basic spec-types, e.g

Types

@type ast_t() :: tuple()

Functions

@spec resolve(ast_t()) :: TypeResolver.Types.t()

This marco takes a spec expression and returns a struct-based representation of the types, resolved to only basic spec-types, e.g:

resolve(integer() | String.t())

returns the following representation:

%TypeResolver.UnionT{
  inner: [%TypeResolver.Types.IntegerT{}, %TypeResolver.Types.BinaryT{}]
}

Raises if types cannot be resolved.