Support guards in Erlang `maybe`

We have finally started playing with maybe expression EEP 49 and we have noticed that they do not support guards. For example, I may want to write:

  {ok, Int} when Int > 0 ?= expr(),

One could argue that the above could be rewritten as:

  {ok, Int} ?= expr(),
  true ?= Int > 0,

But remember guards have specific semantics in relation to errors that would be annoying for the developer to manually replicate.

EEP 49 does not mention guards in this context, so it is not possible to know if this aspect was considered (and then discarded) or not. I can think of two concerns about supporting guards here.

First, the AST currently does not have a place to put guards:


Unless guards are expressed as an operator, which is not done anywhere else in the AST.

The punctuation for multiple guards in Erlang can be confusing. Take this example:

  {ok, Int} when Int > 0, Int < 0 ?= expr(),

One option is to require parenthesis in such cases:

  ({ok, Int} when Int > 0, Int < 0) ?= expr(),

Any thoughts, concerns, or alternatives?


For one, I think guards could be useful there. But…

… looks pretty ugly to me, parentheses or not :fearful: AFAIK, this would be the first thing where guards appear before the expression that actually produces them, everywhere else where we can have guards (function heads, if, case, try, …) they appears afterwards.

So, in a nutshell, I’m for guards here, but please find a better style. (And no, no suggestions from me :sweat:)


I agree.

Given we use ?=, which looks like =, it could also be:

  {ok, Int} ?= expr() when (Int > 0, Int < 0),

But that would also 100% require parentheses and I am not sure it is better.

1 Like

I think Maria was pointing out that you’re using the result of the expression in the guard. Unless I’ve missed something this is not possible anywhere else in the language.

That aside, moving the ‘when’ to the right would match the rest of the language, and since the punctuation-based operators are the problem, there are two options;
a) require parentheses, or
b) don’t allow the punctuation operators (only or/and/… etc)

1 Like

Yeah guards were fully out of scope there, mostly because I wanted the smallest disruption to syntax as possible. The grammar is maybe Exprs | MatchOrReturnExprs end where MatchOrReturnExprs is defined as Pattern ?= Expr.

Guard expressions generally contain the operators ; (“or”) and , (“and”), the latter of which could likely be tricky to read in cases such as:

  %% confusing on the assignment, but only one interpretation
  %% since A > B can't be a pattern
  {ok, Int} when expr(), A > B ?= expr(),
  %% should this be legal? Only if Val is bound or also when unbound?
  _ when Val ?= expr(), Val =:= 3,
  %% Or do you need to flip the clause to ensure the `expr()` is always last?
  %% this would mess some binding rules' expectations at least? 
 _ when Val =:= 3, Val ?= expr(),
 %% That's of course a trap, because `_ [Guards] ?= expr()` is the key there so
 %% the following two lines are equivalent:
 _ when Val =:= 3, Val ?= expr(),
 _ when Val, Val =:= 3 ?= expr(),
 %% So this tells only pre-bound value should be allowed there, so we
 %% instead need the following to get the other intent, which also
 %% risks looking confusing:
 BoundPat when Val =:= 3, BoundPat =:= Val ?= expr(),
 %% Then there's just confusing cases like the following, where it's unclear
 %% if you should error on "A when is_list(A)" being an illegal expression
 %% or on "A = 3" being an illegal guard, depending on how you parse it
 A when is_list(A), A = 3, X ?= expr()

Then if you decide that hey, using , and ; in there is not great for readability so you only use and, or, andalso, and orelse, then you’re defining a whole new guard type (or a type of possibly side-effectful validation expressions like in list comprehensions) and the whole spec’s scope grows and grows.

Overall this felt like it’d bring in so much more confusion than it would bring clarity (especially when my initial intent was to make {ok, Val} | {error, Val} more of a structural language-supported pattern than a convention, which did not happen) that it simply felt not worth even considering.


I wonder if solution would be to introduce “single-expression guards” - guards that explicitly don’t allow , or ; operators and instead require using andalso and orelse.
In particular this would allow such guards to be used easily in this context, or for that matter, any other context where a pattern is supported with no ambiguity.


A regular expression match in a guard would be nice. But then a Reg exp pattern match on the mailbox queue could be fun also.

1 Like

I wold personally go for

{ok,X} when X < 1, is_float(X) ?= expr()

as it keeps the pattern and the guard together.