[erlang-questions] The If expression

Zvi <>
Sun Jan 25 12:01:42 CET 2009

1. I'm in favor of deperectaing if and using some LISP-like cond mechanism
(cond even reserved word), i.e.:

   BoolExpr1 [when Guard1] -> Val1;
   BoolExpr2 [when Guard2] -> Val2;
   BoolExprN [when GuardN] -> ValN;
   [true -> ValOtherwise]

there might be some syntactic sugar for OTHERWISE clause (but better not,
see below).
Also might needed some mechanism for combining several BoolExpr/Guard
clauses with the same Val expression.

2. I miss C/C++/Java like conditional if expresssion:  

   Cond ? Expr1 : Expr2

it's much clearer than Erlang's:

   case Cond of
     true -> Expr1 ;
     false -> Expr2

I using macro like:

 -define(IF(cond,e1,e2), (case (cond) of true -> (e1); false -> (e2) end) ).

but syntactic sugar at the language level would be much better.

3. With all the multicore buzz, I that new speculative parallel control flow
mechanisms are needed, like pcase and pcond:

   pcase Expr of
      Pattern1 [when Guard1] -> Expr1;
      Pattern2 [when Guard2] -> Expr2;
      PatternN [when GuardN] -> ExprN

      Cond1 [when Guard1] -> Expr1;
      Cond2 [when Guard2] -> Expr2;
      CondN [when GuardN] -> ExprN

Here conditions/patterns aren't calculated/matched in the serial orded from
top down, but in parallel and when the first is true or matched it's
corresponding expression is evaluated, thus "else"/"otherwise/->true" clause
is must be avoided. Thus Richard's examples are already multicore
future-proof ;)

         X > Y  -> a();
         X =< Y -> b()

          X > Y -> a();
          X < Y -> b();
          X ==Y -> c();

      pcase math:sign(X-Y) of
         1 -> a();
        -1 -> b();
         0 -> c();

The next part is speculative execution of expressions themselves, but before
there are need to be mechanism of specifing pure functions.


View this message in context: http://www.nabble.com/The-If-expression-tp21244494p21650458.html
Sent from the Erlang Questions mailing list archive at Nabble.com.

More information about the erlang-questions mailing list