Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Erlang Pattern matching with aliases

is there a possibility to match in a function definition do some subset of a touple and still get get complete touple in the method ?

What I would like to do is something like this:

myfun({ foo, Bar }: Var) -> otherfunction(Var, stuff).

instead of:

myfun({ foo, Bar }) -> otherfunction({ foo, Bar }, stuff).

I hope this is clear enough.

Thanks.

like image 800
ptriller Avatar asked Nov 24 '25 08:11

ptriller


1 Answers

You can ignore some parameters by putting an underscore in front. E.g.

myfun( {foo, _Bar, Var } )

will match by ignoring the _Bar parameter. Is that what you had in mind?

Or did you mean:

myfun( {foo, Bar} = Var ) -> otherfun( Var ).

in this case, Var will be used in otherfun iff the match with myfun succeeded. The reason is: Var is un-bound at the time of evaluation of the expression and thus will be assigned to {foo, Bar}.

like image 134
jldupont Avatar answered Nov 28 '25 09:11

jldupont



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!