elixir - 12 alias, require, import

访问量: 167

alias

alias Foo.Bar, as :Bar
alias Foo.Bar  
两者的作用是一样的

iex> is_atom(String)
true
iex> to_string(String)
"Elixir.String"
iex> :"Elixir.String" == String
true

require: 用来引入某个模块

iex(6)> Integer.is_odd(3)
** (CompileError) iex:6: you must require Integer before invoking the macro Integer.is_odd/1
    (elixir) src/elixir_dispatch.erl:97: :elixir_dispatch.dispatch_require/6
iex(6)> require Integer
Integer
iex(7)> Integer.is_odd(3)
true

import 用来引入部分内容 , 会自动调用 require

iex(9)> import Integer
Integer
iex(10)> import Integer, only: :macros
Integer
iex(11)> import Integer, only: :functions
Integer

use (没看出来干嘛的)

下面两个代码 是等价的. (后面的代码是编译后的内容) 
defmodule Example do
  use Feature, option: :value
end
is compiled into

defmodule Example do
  require Feature
  Feature.__using__(option: :value)
end

订阅/RSS Feed

Subscribe

分类/category