Elixir: function overloading with different arity

為{幸葍}努か 提交于 2019-12-10 13:08:45

问题


is there any way to define overload functions with different arity, e.g in C# I can just do:

foo(bar)

or

foo(bar, baz)

In Elixir, the only way to do that would be to put them in separate modules, which will get messy pretty quickly. Is there any way around it?

Edit: I had made a wrong assumption. The examples of overloaded functions I saw happened to have the same arity, so I (wrongly) assumed that this was a requirement. Functions are uniquely identified by their name and arity, so you can in fact overload functions with different arity.


回答1:


In Erlang and Elixir, and unlike many other languages (such as C#), functions are uniquely identified by their name and arity, so technically foo(bar) and foo(bar, baz) are totally different functions. But that's really just a technicality, to write an 'overloaded' function in Elixir, you would write something like the following definition of sum:

defmodule Math do
  def sum(list),       do: sum(list, 0)
  def sum([], acc),    do: acc
  def sum([h|t], acc), do: sum(t, acc + h)
end



回答2:


On this page see especially section 8.3 and following. Specifically this:

Function declarations also support guards and multiple clauses. If a function has several clauses, Elixir will try each clause until it finds one that matches. Here is an implementation of a function that checks if the given number is zero or not:

defmodule Math do
  def zero?(0) do
    true
  end

  def zero?(x) when is_number(x) do
    false
  end
end

Math.zero?(0)  #=> true
Math.zero?(1)  #=> false

Math.zero?([1,2,3])
#=> ** (FunctionClauseError)

Same function name with multiple overloads (although the concept is called clauses in the documentation) in a single module.



来源:https://stackoverflow.com/questions/23600513/elixir-function-overloading-with-different-arity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!