Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

More than one match in case statement in Erlang?

Tags:

case

erlang

I have that kind piece of code:

case sth of
    {a, 1} -> doA();
    {a, 2} -> doA();
    {a, 3} -> doB()
end.

Is there a way not to repeat "doA()" part? I thought that it should be easy, but I couldn't found answer in google.

like image 616
zie1ony Avatar asked Jul 08 '12 01:07

zie1ony


1 Answers

You can use when guards in the case statement such as:

case sth of
    {a, Var} when Var < 3-> doA();
    {a, 3} -> doB()
end.

Also your expression(sth) is an atom here meaning it can never match any of those cases.

like image 62
Scott Logan Avatar answered Nov 09 '22 01:11

Scott Logan