使用模块
模块有四个相关的关键字,可以在其他模块中使用它们:alias,import,use 和 require。
alias 将以不同(通常更短)的名称注册模块:
defmodule MyModule do
# Will make this module available as `CoolFunctions`
alias MyOtherModule.CoolFunctions
# Or you can specify the name to use
alias MyOtherModule.CoolFunctions, as: CoolFuncs
end
import 将使模块中的所有功能都可用,前面没有名字:
defmodule MyModule do
import Enum
def do_things(some_list) do
# No need for the `Enum.` prefix
join(some_list, " ")
end
end
use 允许模块将代码注入当前模块 - 这通常作为框架的一部分完成,该框架创建自己的函数以使模块确认某些行为。
require 从模块加载宏,以便可以使用它们。