newbie question

Matthias Lang <>
Tue May 17 13:19:52 CEST 2005


>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