Skip to content

andytill/ohmyguard

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ohmyguard

Parse transform for converting my_func(X) when is_binary(X) -> X to my_func(X/binary) -> X..

Why?

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.

Syntax that still needs to be implemented...

Case statements
case V of
	{ok, A/binary} -> A
end
Assignments
{ok, V/binary} = application:get_env(my_prop)
And much more...

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.

Should I use it?

No way! It is just a proof of concept at the moment.

About

Binary patten matching style syntax for erlang function guards

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published