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

Raimo Niskanen raimo+erlang-questions@REDACTED
Tue Jun 16 12:32:59 CEST 2009


On Tue, Jun 16, 2009 at 11:32:39AM +0200, info wrote:
> But we can with the proposed solution:
> 
> case Expr of
> X when X=:=a; X=.=b ->block1;
> c->block2
> end

Note:

> > The question was "is it possible to group two patterns in a case ?"

For patterns it is not possible.

X=:=a; X=:=b are not patterns, they are guard clauses.
With patterns it could be something like this:

case Expr of
    X={ok,A} when is_integer(A) ->
    X={error,B} when is_atom(B) ->
	block1; % Is A or B bound? X should be.
    c ->
	block2
end

Of course that particular example can be done with guard clauses like this:

case Expr of
    X when tuple_size(X)=:=2, element(1,X)=:=ok, is_integer(element(2,X));
           tuple_size(X)=:=2, element(1,X)=:=error, is_atom(element(2,X)) ->
	block1;
    c ->
	block2
end

...but that is practically unreadable compared to the
example that does not compile. And for complex patterns it
becomes hopeless.

> 
> 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.
> 
:

-- 

/ Raimo Niskanen, Erlang/OTP, Ericsson AB


More information about the erlang-questions mailing list