DEV Community

Discussion on: I've never become overly convinced that switch statements are that much cleaner than `if else if else if else if else`

Collapse
 
giovannicodes profile image
Giovanni Cortés • Edited

Elixir :)

def my_switcher("a"), do: 1
def my_switcher("b"), do: 2
def my_switcher(), do: 3

iex(1)> Example.my_switcher("a")
1
iex(2)> Example.my_switcher("b")
2
iex(3)> Example.my_switcher()
3
Enter fullscreen mode Exit fullscreen mode
Collapse
 
kip13 profile image
kip

Pattern matching to the rescue ?

Thread Thread
 
cescquintero profile image
Francisco Quintero 🇨🇴

Isn't this feature called method overloading ? 🤔

Thread Thread
 
giovannicodes profile image
Giovanni Cortés

No, because overloading is have same parameters with different types, but in patter matching you can have the same type and number of parameters but differs in the content. For example, here!

Collapse
 
seanwash profile image
Sean Washington

I quite like cond as well for certain situations as well.