Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/iequ1/typerefl
Use Erlang typespecs in the runtime
https://github.com/iequ1/typerefl
erlang reflection typechecker
Last synced: 3 months ago
JSON representation
Use Erlang typespecs in the runtime
- Host: GitHub
- URL: https://github.com/iequ1/typerefl
- Owner: ieQu1
- License: unlicense
- Created: 2019-05-02T20:33:31.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2024-01-14T17:32:23.000Z (about 1 year ago)
- Last Synced: 2024-01-14T22:29:33.260Z (about 1 year ago)
- Topics: erlang, reflection, typechecker
- Language: Erlang
- Homepage:
- Size: 88.9 KB
- Stars: 37
- Watchers: 4
- Forks: 10
- Open Issues: 8
-
Metadata Files:
- Readme: README.org
- License: LICENSE
Awesome Lists containing this project
README
#+TITLE: Typerefl
This library reifies dialyzer types as ordinary Erlang functions. It
enables e.g. validation of terms against dialyzer typespecs in the
runtime.* Talk is cheap, show me the code
Simple example using builtin types:
#+BEGIN_SRC erlang
-include_lib("typerefl/include/types.hrl").main() ->
%% Define type (in the term Universe!)
Type = {foo, non_neg_integer(), #{atom() => integer()}},
%% Valid term:
ok = typerefl:typecheck(Type, {foo, 3, #{bar => 42}}),
%% Invalid term:
{error,"Expected: {foo, non_neg_integer(), #{atom() => integer()}}\nGot: 1\n"} =
typerefl:typecheck(Type, 1).
#+END_SRCHigher kind recursive types are of course supported too:
#+BEGIN_SRC erlang
-include_lib("typerefl/include/types.hrl").-type stupid_list(A, B) :: {cons, A, stupid_list(A, B)} | B.
-type stupid_list(A) :: stupid_list(A, nil).
%% Any dialyzer type can be reified:
-reflect_type([stupid_list/1]).main() ->
ok = typerefl:typecheck(stupid_list(atom()), {cons, foo, nil}),
{error, _} = typerefl:typecheck(stupid_list(atom()), {cons, 1, nil}).
#+END_SRCMaps:
#+BEGIN_SRC erlang
-include_lib("typerefl/include/types.hrl").
-type foo() :: #{ foo := integer()
, bar := atom()
, integer() => list()
}.-reflect_type([foo/0]).
main() ->
ok = typerefl:typecheck(foo(), #{foo => 1, bar => foo, 1 => []}).
#+END_SRCThis library typechecks pretty much anything that dialyzer does,
except for functions. In the future it may support arity check.