1

I am new Elixir and really enjoying it. I hit a wall when trying to use functions with same name. Here is an example

defmodule ChangeName do
  def convert(:captilize, name), do:  String.capitalize(name)
  def convert(:lower, name), do: String.downcase(name)
end

I am using iex and the basic calls where ChangeName.convert.captilize but how do I run these functions?

Thanks

Nair
  • 7,438
  • 10
  • 41
  • 69
  • Also see this Q & A for more detail on this: http://stackoverflow.com/questions/23600513/elixir-function-overloading-with-different-arity – Onorio Catenacci Mar 04 '15 at 19:57

1 Answers1

6

The example you give doesn't define two functions with the same name, but a single multiclause function. It is roughly equivalent to:

defmodule ChangeName do
  def convert(conversion, name) do
    case conversion do
      :capitalize -> String.capitalize(name)
      :lower -> String.downcase(name)
    end
  end
end

And is called accordingly:

ChangeName.convert(:capitalize, "john")
ChangeName.convert(:lower, "JOHN")

In fact in Erlang it's impossible to define two functions that have the same name and arity.

Paweł Obrok
  • 22,568
  • 8
  • 74
  • 70