假设我正在像这样测试属于Utils模块的功能
defmodule Test do
alias Module.Utils
test "test 1" do
Utils.some_function?(...)
end
test "test 2" do
Utils.some_function?(...)
end
end
我可以将该函数重构或简化为:
import Utils.some_function as test_func()
所以我不必写模块名称并简化函数名称
导入功能时,不能重命名功能。
您可以defdelegate
用来创建一个调用另一个模块的函数的本地函数,如下所示:
defmodule A do
def just_add_these_two_numbers(a, b), do: a + b
end
defmodule B do
defdelegate add(a, b), to: A, as: :just_add_these_two_numbers
# `add/2` is now the same as `A.just_add_these_two_numbers/2`.
def test do
IO.inspect add(1, 2) == 3
end
end
B.test #=> true
尽管您也可以这样做(甚至更短):
def add(a, b), do: A.just_add_these_two_numbers(a, b)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句