[erlang-questions] Re: Correct syntax of a case clause

Raimo Niskanen raimo+erlang-questions@REDACTED
Tue Jun 16 10:25:44 CEST 2009


On Mon, Jun 15, 2009 at 07:17:42PM +0200, info wrote:
> Hi All,
> 
> Hey that was my question .... and you wander from my subject !
> I gave an example but don't try to solve this example !!
> 
> case Expr of
> pattern1 -> do_1;
> pattern2-> do_2;
> pattern3 -> do_3;
> ...
> end
> 
> The question was "is it possible to group two patterns in a case ?"

No it is not.

Because two general patterns "pattern1" and "pattern2" may bind
different variables making it unclear which variables
are bound in the common body, and instead of forcing
the limitation on the two general patterns that they
must bind exactly the same sets of variables,
which would be possible but would complicate the compiler,
not to mention if you add "when" clauses to both patterns;
it was decided to not allow multiple patterns on one body.

Workarounds have already been given in this thread.

> 
> In another pseudo code:
> if Expr = pattern1 OR Expr = pattern2 then do_1;
> if Expr = pattern3 then ...
> ...
> 
> case Expr of
> {pattern1,pattern2} -> do_1;
> pattern3 -> do_3;
> ...
> end
> ... doesnt work ... normal !
> case Expr of
> (pattern1,pattern2)-> do_1;
> ...
> end
> ...doesn't work
> 
> 
:

-- 

/ Raimo Niskanen, Erlang/OTP, Ericsson AB


More information about the erlang-questions mailing list