Guards on assignment (i.e. assertions)

Garrett Smith <>
Mon Mar 1 21:00:26 CET 2010

In most cases, assertions come "free" in Erlang in guards and pattern
matching. There are cases though where I'd like to apply a guard a
statement like this:

  A = B when B > C

I currently do this:

  A = case B of ValidB when ValidB > C -> ValidB end

or, a simpler form (but the error message on failure isn't as clear):

  A = if B > C -> B end

There are other ways as well, e.g. using functions, etc. but I like
the first form.

I'm wondering how others implement basic assertions on assignments. I
think if the first form were supported by the language, I'd use the
"assert" pattern and my code would be better for it.


More information about the erlang-questions mailing list