模式匹配不同模块中具有相同名称的函数

时间:2017-03-06 18:59:10

标签: elixir

我想扩展flash消息的类型,但是当我执行以下操作时出现错误:

defmodule Backend.AuthView do
  use Backend.Web, :view

  def flash(message, :auth_info) do
    ...
  end
  def flash(message, :auth_notice) do
   ... 
  end
end

并将其导入另一个模块:

defmodule Backend.LayoutView do
  use Backend.Web, :view
  import Backend.AuthView

  def flashes(conn) do
    markup safe: true do
      messages = Enum.reduce [:notice, :error], [], fn(which, acc) ->
        case get_flash(conn, which) do
          nil -> acc
          _ -> acc ++ [{ which, get_flash(conn, which)}]
        end
      end
      if messages != [] do
        Enum.map messages, fn({which, message}) ->
          flash message, which
        end
      end
    end
  end
  def flash(message, :info) do
    ...
  end
  def flash(message, :notice) do
    ...
  end
end

是否有可能让这样的事情发生?

1 个答案:

答案 0 :(得分:0)

您可以将导入更改为别名,并实现回退功能,如此

defmodule Backend.LayoutView do
  use Backend.Web, :view
  alias Backend.AuthView

  def flash(message, :info) do
    ...
  end

  def flash(message, :notice) do
    ...
  end

  def flash(message, other) when is_atom(other) do
    AuthView.flash(message, other)
  end
end