Polymorphism in Elixir
Mustafa Turan
491

Yes it’s cool, and I know this article for describe the protocol feature of Elixir, but I can not resist to rewrite this as follows :)

defmodule Temperature do
def to_celsius(%Kelvin{degree: degree}), do: %Celsius{degree: degree - 273.15}
def to_celsius(%Fahrenheit{degree: degree}), do: %Celsius{degree: (degree - 32) * 5 / 9}
def to_celsius(degree), do: raise(ArgumentError, message: "There are no convertion to celsius for '#{to_string(degree)}''")
end

All in one place. Short.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.