如果一个模块import
由于有多个其他模块,因此给定函数的来源可能并不明显。例如:
defmodule Aimable do
import Camera
import Gun
def trigger do
shoot # which import brought it in?
end
end
我知道有一些方法可以最大限度地减少这种混乱:良好的命名、集中的模块、有针对性的导入,例如import Gun, only: [:shoot]
, etc.
但是如果我遇到这样的代码,有没有办法检查Aimable
并查看该函数的起源shoot
?
您可以直接执行此操作:
# from inside the module; IO.inspect(&Aimable.shoot/0) reveals nothing
IO.inspect &shoot/0 #=> &Gun.shoot/0
看一下这个 https://stackoverflow.com/questions/32648894/how-to-determine-from-which-module-a-specific-function-was-imported-in-elixir
另请记住,您不能在两个不同的模块中具有相同的函数名称和相同的数量,并将它们都导入到另一个模块中。这将导致调用该函数时出现歧义错误。
另一种痛苦的方式。您可以使用函数_导出?/3。 http://elixir-lang.org/docs/v1.0/elixir/Kernel.html#function_exported?/3. Specs:
function_exported?(atom | tuple, atom, arity) :: boolean
如果模块已加载并包含具有给定数量的公共函数,则返回 true,否则返回 false。
例子:
function_exported?(Gun, :shoot, 0) #=> true
function_exported?(Camera, :shoot, 0) #=> false
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)