How to deal with the simple possibility that an integer is passed to String.to_integer(string)

I want to convert strings to integers. String.to_integer(string) works when "2" is given to it but raises error when 2 is passed to it.

How to deal with the input if it is already an integer ?

defp to_int(int) when is_integer(int), do: int
defp to_int(str) when is_binary(str), do: String.to_integer(str)
3 Likes