SV: newbie question

Lennart Öhman <>
Tue May 17 13:57:07 CEST 2005


One significant difference when it comes to side-effects is
that in 'case' you will *always* pass through that single "thing"
causing a side effect. In ifs, in contrast, you may execute one
or several if-clauses depending on if and when an if-clause
turns true. Making the latter much more difficult to follow.
 
Best Regards
Lennart
 
case function_having_side_effects(X) of
   foo ->
      ....;
   bar ->
      ....
end,
 
if
    one_function_having_side_effect(X)==foo ->
        ....
    another_function_with_side_effects(X)/=dog ->
        ....
    even_more_sideeffects(X)>10 ->
        ....;
    true ->
        i_have_done_all_side_effects
end.
 
 
-------------------------------------------------------------
Lennart Ohman                   phone   : +46-8-587 623 27
Sjöland & Thyselius Telecom AB  cellular: +46-70-552 6735
Sehlstedtsgatan 6               fax     : +46-8-667 8230
SE-115 28 STOCKHOLM, SWEDEN     email   : 

________________________________

Från:  genom Thomas Johnsson XA (LN/EAB)
Skickat: ti 2005-05-17 13:37
Till: 
Ämne: RE: newbie question



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: 
[mailto:]On Behalf Of Matthias Lang
Sent: den 17 maj 2005 13:20
To: Dietmar Schaefer
Cc: 
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


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20050517/cdfaebcd/attachment.html>


More information about the erlang-questions mailing list