[erlang-questions] pattern match test operator

Vlad Dumitrescu vladdu55@REDACTED
Fri Nov 16 22:00:26 CET 2007


Hi!

On Nov 16, 2007 8:07 PM, Andras Georgy Bekes <bekesa@REDACTED> wrote:
> I'm missing a pattern match test operator from Erlang. What I mean?
> I think there should be a built-in operator, which requires a pattern on
> the left-side and a value on the right side, and returns true or false,
> depending on the success of the match.

Unless there is some use case I can't see right now, the constructs
you are suggesting are not really needed.

More precisely, I can't see when I would want to do a match against a
pattern containing only '_'s and if that is true I wouldn't need to
match again to retrieve some of the internal terms later on. In this
case, it's better to match even the internal structure from the start.

* In list comprehensions, the filtering is done automagically:
  [ X || {X, _} <- L]
will simply ignore elements in L that aren't of the form {_, _}.

* One could rewrite

  case Value1 of
     PATTERN1 ->
         case Value2 of
             PATTERN2 ->
                 this;
             _ ->
                 that
         end;
     _->
         that
  end

as

  case {Value1, Value2} of
     {PATTERN1, PATTERN2} ->
           this;
     _->
          that
  end

* Regarding
      fun(Arg) when ?MATCH(PATTERN1,Arg) or ?MATCH(PATTERN2,Arg) ->
I'm not sure why you want to match Arg against two patterns. If you meant
      fun(Arg1, Arg2) when ?MATCH(PATTERN1,Arg1) or ?MATCH(PATTERN2,Arg2) ->
then the usual way to do that is
      fun(PATTERN1, PATTERN2) ->

So I personally don't need a match operator, it is already omnipresent
in the language.

best regards,
Vlad



More information about the erlang-questions mailing list