View Source TypeCheck.Type (TypeCheck v0.13.5)

TODO

Link to this section Summary

Types

Indicates that we expect a 'type AST' that will be expanded to a proper type. This means that it might contain essentially the full syntax that Elixir Typespecs allow, which will be rewritten to calls to the functions in TypeCheck.Builtin.

t()

Something is a TypeCheck.Type if it implements the TypeCheck.Protocols.ToCheck protocol.

Functions

Constructs a concrete type from the given type_ast.

Link to this section Types

@type expandable_type() :: any()

Indicates that we expect a 'type AST' that will be expanded to a proper type. This means that it might contain essentially the full syntax that Elixir Typespecs allow, which will be rewritten to calls to the functions in TypeCheck.Builtin.

See TypeCheck.Builtin for the precise syntax you are allowed to use.

@type t() :: x :: any()

Something is a TypeCheck.Type if it implements the TypeCheck.Protocols.ToCheck protocol.

It is also expected to implement the TypeCheck.Protocols.Inspect protocol (although that has an Any fallback).

In practice, this type means 'any of the' structs in the TypeCheck.Builtin.* modules.

(This type is managed by TypeCheck, which allows checking values against the type at runtime.)

Full definition:

t() :: wrap_with_gen(
  x :: any() when TypeCheck.Type.type?(x),
  &TypeCheck.Type.StreamData.arbitrary_type_gen/0
)

Link to this section Functions

Link to this macro

build(type_ast, options \\ TypeCheck.Options.new())

View Source (macro)

Constructs a concrete type from the given type_ast.

This means that you can pass type-syntax to this macro, which will be transformed into explicit calls to the functions in TypeCheck.Builtin.

iex> res = TypeCheck.Type.build(:ok | :error)
iex> res
#TypeCheck.Type< :ok | :error >
iex> # This is the same as:
iex> import TypeCheck.Builtin, only: [one_of: 2, literal: 1]
iex> explicit = one_of(literal(:ok), literal(:error))
iex> res == explicit
true

iex> res = TypeCheck.Type.build({a :: number(), b :: number()} when a <= b)
iex> res
#TypeCheck.Type< ({a :: number(), b :: number()} when a <= b) >
iex> # This is the same as:
iex> import TypeCheck.Builtin, only: [fixed_tuple: 1, number: 0, guarded_by: 2, named_type: 2]
iex> explicit = guarded_by(fixed_tuple([named_type(:a, number()), named_type(:b, number())]), quote do a <= b end)
iex> explicit
#TypeCheck.Type< ({a :: number(), b :: number()} when a <= b) >

Of course, you can refer to your own local and remote types as well.