''illegal pattern'' when using records in case statement

Richard Carlsson <>
Thu Feb 6 11:47:04 CET 2003


On Thu, 6 Feb 2003, Bengt Kleberg wrote:

> i would like to do the following:
> 
> dpid_to_if(Dp, Domain) ->
>     case Dp of
> 	Domain#prswDomain.working_dp ->
> 	    Domain#prswDomain.working_if;
> 	Domain#prswDomain.protection_dp ->
> 	    Domain#prswDomain.protection_if
>     end.
> 
> i know the workaround, but why is this an illegal pattern?

What your attempted pattern wants to test (i guess) is that "in case Dp
is *the 'working_dp' element of* the 'prswDomain'-record Domain ...",
but that would be equivalent to:

	case Dp of
	    element(5, Domain) -> ...

To be more formalistic about why that can't be made into a pattern:
patterns describe the static skeleton of the data (counting pre-bound
variables as static), and a pattern like this one would not be static.

This version uses guards, which are normal dynamic computations,
although they have some other restrictions:

	if Dp == Domain#prswDomain.working_dp -> ...;
	   Dp == Domain#prswDomain.protection_dp -> ...
	end

This version pre-extracts the fields and binds them to variables:

dpid_to_if(Dp, Domain#prswDomain{working_dp = W, protection_dp = P) ->
    case Dp of
	W ->
	    Domain#prswDomain.working_if;
	P ->
	    Domain#prswDomain.protection_if
    end.

	/Richard


Richard Carlsson ()   (This space intentionally left blank.)
E-mail: 	WWW: http://user.it.uu.se/~richardc/




More information about the erlang-questions mailing list