How to define a wildcard pattern using cerl:c_clause-Collection of common programming errors

I’m trying to compile some personal language to erlang. I want to create a function with pattern matching on clauses.

This is my data :

Data =
    [ {a,  }
    , {b,  }
    , {c,  }
    ].

This is what i want :

foo(a) -> ;
foo(b) -> ;
foo(c) -> ;
foo(_) -> undefined. %% 
        cerl:c_clause([cerl:c_atom(Pattern)], deep_literal(Body))
    end,
WildCardClause = cerl:c_clause([ ??? ], cerl:c_atom(undefined)),
CaseClauses = [MkCaseClause(S) || S 
        case Key of
           when 'true' -> ...
           when 'true' -> ...
           when 'true' -> ...
        end

So okay, case is translated to if when core is compiled. So i need to specify a true clause as in an if expression to get a pure wildcard. I don’t know how to do it, since matching true in an if expression and in a case one are different semantics. In a case, true is not a wildcard.

And what if i would like match expressions with wildcards inside like {sometag,_,_,Thing} -> {ok, Thing}.

Thank you

  1. I’ve found a way to do this

    ...
    WildCardVar = cerl:c_var('_Any'),
    WildCardClause = cerl:c_clause([WildCardVar], cerl:c_atom(undefined)),
    ...
    

    It should work for inner wildcards too, but one has to be careful to give different variable names to each _ wildcard since only multiple _ do not match each other, variables do.

    f(X,_, _ ) %% matches f(a,b,c)
    f(X,_X,_X) %% doesn't
    

Originally posted 2013-11-09 23:29:24.