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

Richard O'Keefe <>
Wed Jun 17 03:53:08 CEST 2009


On 16 Jun 2009, at 10:32 pm, Raimo Niskanen wrote:

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

Isn't there a proposal to allow

	case Expr
	  of P11 when G11
            ; P12 when G12 ->
		B1
	   ; P21 when G21
	   ; P22 when G22 ->
		B2
	...
	end

It isn't currently in Erlang, but it should be no big deal to add it.
(Proof of possibility:  simply copy the bodies.)

However, it has been pointed out before that you can split this
into
	classify(P11) when G11 -> {a,Vars1};
	classify(P12) when G12 -> {a,Vars1};
	classify(P21) when G21 -> {b,Vars2};
	classify(P22) when G22 -> {b,Vars2};
	...

	case classify(Expr)
	  of {a,Vars1} -> B1
	   ; {b,Vars2} -> B2
	 ...
	end

and that if you want two patterns handled together in one case,
you probably want them handled together in others, so the
classify/1 function may well be reusable.  In any case, it
will be inlinable.

So the answer to the question "is it possible to group two
patterns together in a case" is "yes, but not DIRECTLY, not yet."



More information about the erlang-questions mailing list