[erlang-questions] Guards on assignment (i.e. assertions)

David Mercer <>
Mon Mar 1 21:34:54 CET 2010


That seems like a legitimate use of the "if" keyword.

	A = if B > C -> B end

Or

	if B > C -> A = B end

> -----Original Message-----
> From:  [mailto:] On
> Behalf Of Garrett Smith
> Sent: Monday, March 01, 2010 2:00 PM
> To: Erlang Questions
> Subject: [erlang-questions] Guards on assignment (i.e. assertions)
> 
> 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.
> 
> Garrett
> 
> ________________________________________________________________
> erlang-questions (at) erlang.org mailing list.
> See http://www.erlang.org/faq.html
> To unsubscribe; mailto:



More information about the erlang-questions mailing list