newbie question

Thomas Johnsson XA (LN/EAB) thomas.xa.johnsson@REDACTED
Tue May 17 13:37:49 CEST 2005


There are situations where it is important that guards are fast and side effect-free (in receive, for example).
Conditions in if-expressions is *not* one of those places, IMHO. (Except for the usual good reasons to 
to program as 'functionally' as possible).
Ie, there is no reason that I can see why the if-condition has to be a guard syntactically rather than an expression.
I just forces the programmer to write something more clumsy.

Or is there some light I haven't yet seen?
-- Thomas

-----Original Message-----
From: owner-erlang-questions@REDACTED
[mailto:owner-erlang-questions@REDACTED]On Behalf Of Matthias Lang
Sent: den 17 maj 2005 13:20
To: Dietmar Schaefer
Cc: erlang-questions@REDACTED
Subject: Re: newbie question


>From the FAQ:

  | 9.4. Why can't I call arbitrary functions in a guard?
  | 
  | If that was allowed, there would be no guarantee that guards were side-effect free.
  | 
  | Also, it is convenient to be able to program as though guards do not consume any significant amount of execution time. There's a list of BIFs which can be called from within guards in the Erlang book and the standard Erlang spec, some examples are size(), length(), integer(), record().
  | 
  | The "problem" often crops up when using if:
  | 
  | 	issue_warning() ->
  | 	  if (os:type() == {win32, windows}) ->    %% illegal guard
  | 	    ok = io:fwrite("this computer may crash at any time\n");
  | 	  true ->
  | 	    ok = io:fwrite("no problem\n")
  | 	  end.
  | 	
  | 
  | The solution is usually to use case instead. Case is used much more frequently than if in most Erlang programs:
  | 
  | 	issue_warning() ->
  |           case os:type() of
  |             {win32, windows} -> ok = io:fwrite("crash soon\n");
  |             _ -> ok = io:fwrite("no problem\n")
  | 	  end.
  | 	

Matthias

Dietmar Schaefer writes:
 > Hi !
 > 
 > Isn't that supposed to work ?
 > 
 > 
 > if  cmmc_db:getProcState(Key,Name) /= State -> 
 > 
 > instead I have to write
 > 
 > ProcState = cmmc_db:getProcState(Key,Name),
 > 
 > if ProcState /= State ->
 > 
 > 
 > 
 > regards
 > 
 > 
 > Dietmar



More information about the erlang-questions mailing list