You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Such function would be useful for custom guards in Elixir (and probably other languages that allow defining such). Example could be {{is_struct/2}} in Elixir which would fail if the second argument is non-atom. Current hack involves using function that will always fail, ex.:
{code:ruby}
defguard is_struct(struct, type)
when is_map(struct) and (is_atom(type) or hd([]))
and :erlang.map_get(:__struct__, struct) == type
{code}
However that seems pretty unidiomatic. Having function like {{erlang:guard_fail/0}} would make it more readable and would show the intended behaviour.
The text was updated successfully, but these errors were encountered:
There does not seem to be much use for a {{guard_fail/0}} BIF for Erlang programs, and for Elixir it seems that you could easily add your own {{guard_fail/0}} function that expands to an expression that will fail in a guard.
By the way, there is another way to make a guard fail. Perhaps this is clearer:
{code:java}
defguard is_struct(struct, type)
when is_map(struct) and (is_atom(type) or :fail)
and :erlang.map_get(:__struct__, struct) == type
{code}
This works because a non-boolean value as the left operand of any boolean operation ({{and}}, {{andalso}}, and so on) will cause the entire guard to fail.
Original reporter:
hauleth
Affected version:
Not Specified
Component:
Not Specified
Migrated from: https://bugs.erlang.org/browse/ERL-1122
The text was updated successfully, but these errors were encountered: