[erlang-questions] Floating guard sequences

Michael Radford mrad-direct-erlang@REDACTED
Fri Feb 20 07:59:20 CET 2009


Richard O'Keefe writes:
> It's sometimes overlooked that 'if' doesn't need more than one
> clause.  So we can always write
> 
> 	{X,[$x|Rest]} = string:to_integer(Str),
> 	if is_integer(X) -> true end,
> 	{Y,_        } = string:to_integer(Rest)

Of course this is a matter of taste, but I would tend to write the
second line as
	true = is_integer(X),
which emphasizes its nature as an assertion.  (I really do write lines
like that on a fairly regular basis, esp. in test code.)

Unfortunately the most helpful error message, when the assertion
fails, would be given by
	case X of _ when is_integer(X) -> ok end,
but that's ugly enough that one would be tempted to hide it with a
macro.

Mike



More information about the erlang-questions mailing list