Parse transform for converting my_func(X) when is_binary(X) -> X
to my_func(X/binary) -> X.
.
Erlang binary pattern matching is one of its best features and its most succinct syntax, so why not apply it to function guards as well? Given this function:
my_func(Value) when is_binary(Value) ->
Value.
It can be reduced to this:
my_func(Value/binary) ->
Value.
ohmyguard supports all erlang types:
omg_atom(X/atom) -> X.
omg_binary(X/binary) -> X.
omg_bitstring(X/bitstring) -> X.
omg_float(X/float) -> X.
omg_function(X/function) -> X.
omg_integer(X/integer) -> X.
omg_list(X/list) -> X.
omg_map(X/map) -> X.
omg_number(X/number) -> X.
omg_pid(X/pid) -> X.
omg_port(X/port) -> X.
omg_reference(X/reference) -> X.
omg_ref(X/ref) -> X.
omg_tuple(X/tuple) -> X.
The syntax proves even more concise when a function has multiple arguments that need to be checked:
omg_pid_atom(X/pid, Y/atom) -> {X, Y}.
The andalso
and orelse
keywords and confusing commas and semi colons are gone and the function is more readable. All type guards must be met or a function_clause
error is thrown.
Records are also supported, although the syntax is still being worked on:
-record(myrec, {field = value}).
omg_record(Rec/#myrec{}) -> Rec.
It would be better if record guards looked like f(Rec/#myrec) -> Rec.
but this throws a parse error before it can be given to the ohmyguard parse transform. Suggestions welcome!
ohmyguard will not completely replace traditional guards, it is just for type checking.
case V of
{ok, A/binary} -> A
end
{ok, V/binary} = application:get_env(my_prop)
It would be nice if ohmyguard had integration with the recless parse transform, to allow syntax like:
address_street(Address/#address) ->
Address.street.
Also funs, list comprehensions, the mind boggles.
No way! It is just a proof of concept at the moment.