[erlang-questions] Pattern matching in function calls

Antoine Koener antoine.koener@REDACTED
Wed Apr 13 14:46:08 CEST 2011


On Apr 13, 2011, at 13:44 , Dave Challis wrote:

> Does the order of patterns in a function call matter?  Are they all  
> tested, or will erlang stop trying to match them once a mismatch has  
> been found?
>
> As an example, if I've got:
>
> foo(<<SomeLargeBinary>>, a) -> a;
> foo(<<SomeLargeBinary>>, b) -> b.
>
> will the above be any slower than defining:
>
> foo(a, <<SomeLargeBinary>>) -> a;
> foo(b, <<SomeLargeBinary>>) -> b.
>
> Will the <<SomeLargeBinary>> attempt to be matched in every case?   
> Is it something worth thinking about, or will the compiler optimise  
> this?

If the content of <<SomeLargeBinary>> is irrelevant,  you can use the  
'_' notation:

foo(a, _Bin) -> a;
foo(b, _Bin) -> b;

Then this is explicit for the reader that you don't want to check _Bin.





More information about the erlang-questions mailing list