Elixir 编程Elixir & PhoenixElixir

Elixir 简明笔记(十三) --- 模块指令

2016-03-30  本文已影响817人  人世间

Elixir 模块指令

Elixir的模块能够给开发者提供更好的代码组织。可是在不同的模块中使用模块函数,还涉及到一些模块指令。Elixir提供了好几种使用导入模块的方式,importrequireusealias这些都可以对模块进行应用。下面就来尝试他们分别有什么用途。

import 导入模块

在同一个模块使用函数,直接应用函数名即可。在不同的模块中,则需要导入函数。导入函数的方式很简单,只需要使用import引用模块,该模块下的所有公有函数(def定义的函数)就当前模块域可见了。例如之前我们已经见识到了:

geometry/geometry.ex


defmodule Geometry do
    def whoIam? do
        IO.puts __MODULE__
    end

    def get_rectangle do
        IO.puts "rectangle"
    end
end

main.ex


import Geometry

whoIam?

get_rectangle


elixirc会编译整个文件夹,可以针对整个文件夹进行编译。elixirc geometry,运行 main.ex之后得到如下:

☁  learn-elixir  elixir main.ex
Elixir.Geometry
rectangle

import会导入当前所有的函数,如果只向导入特点的函数,只需要传递一个 only的关键字列表:


import Geometry, only: [whoIam?: 0]

whoIam?

# get_rectangle

如果不注释 get_rectangle ,运行 main.ex的时候将会抛出编译错误,编译的时候找不到get_rectangle/0函数。only之后是一个关键字列表,其中第一个key是函数名,value是参数的个数。import MODULE, only: [function_name: airty]

此外,import还是针对模块只引入函数或者宏:

import MODULE, only: :function  #  只引入模块中的所有公有函数

import MODULE, only: :macros    #  只引入模块中的宏

only 是导入指定的函数,含有另外一个用法 import MODULE, except: [whoIam?: 0],指导入除了 whoIam?/0 以外的所有其他公有函数。

alias 别名

import 可以导入模块的所有函数,如果两个模块具有相同的函数呢?修改 geometry/rectangle/rectangle.ex

defmodule Geometry.Rectangle do
    def area(a, b) do
        a * b
    end

    def whoIam? do
        IO.puts __MODULE__
    end

end

main.ex

import Geometry, only: whoIam?

import Geometry.Rectangle

whoIam?

whoIam?

执行之后将会得到下面错误:

☁  learn-elixir  elixir main.ex
** (CompileError) main.ex:6: function whoIam?/0 imported from both Geometry.Rectangle and Geometry, call is ambiguous
    (elixir) src/elixir_dispatch.erl:110: :elixir_dispatch.expand_import/6
    (elixir) src/elixir_dispatch.erl:81: :elixir_dispatch.dispatch_import/5
    (stdlib) lists.erl:1352: :lists.mapfoldl/3
    (stdlib) lists.erl:1353: :lists.mapfoldl/3

elixir编译器无法得知到底是那个模块的whoIam?函数,从而报错。很多别的语言后导入的模块可以覆盖,这一点我更喜好直接抛出错误的方式。既然出错了,就需要解决冲突。可以使用elixir的alias别名指令。修改 main.ex

import Geometry, only: whoIam?

alias Geometry.Rectangle

whoIam?

Rectangle.whoIam?

执行 main.ex 将会得到正确的结果

☁  learn-elixir  elixir main.ex
Elixir.Geometry
Elixir.Geometry.Rectangle

alias Geometry.Rectangle 这句的含义等价于 alias Geometry.Rectangle, as: Rectangle。等于将模块 Geometry.Rectangle 重新命名为 Rectangle,当然 as 也可以自定义别的名字,不然如果出现 Geometry.RectangleGeometry.Other.Rectangle自动命名也会产生冲突。与import不同,alias并不会导入模块中的函数,调用模块的函数需要使用alias的模块别名引用函数调用,即 AliasModule.function_name

USE

在使用 elixir的一些web框架,例如Plug的时候,经常会看到使用use的对模块的操作。use其实很简单,它除了在Module上调用__using__宏外, 不做其他任何事情. 例如:

geometry/geometry.ex

defmodule Geometry do

    defmacro __using__(_opts) do
        IO.puts "use #{__MODULE__}"
    end

    def whoIam? do
        IO.puts __MODULE__
    end
end

main.ex

use Geometry

编译运行得到输入为:


☁  learn-elixir  elixir main.ex
use Elixir.Geometry

可以比较一下和 import 的差别,修改main.ex

use Geometry
whoIam?

可以看到输出错误,编译找不到 whoIam? 函数

☁  learn-elixir  elixir main.ex
use Elixir.Geometry
** (CompileError) main.ex:4: undefined function whoIam?/0
    (elixir) expanding macro: Kernel.to_string/1
    main.ex:4: (file)

再次修改 geometry.ex, 在 `using 宏中导入当前模块。

defmodule Geometry do

    defmacro __using__(_opts) do
        quote do
            import Geometry
        end
    end

    def whoIam? do
        IO.puts __MODULE__
    end
end

编译运行,可以发现main得到了正确的结果,当使用 use Geometry的时候,调用了Geometry的__using__宏,__using__又展开了导入Geometry。因此等于在main中执行了import Geometry 的操作。

require

require 允许使用一个模块的宏, 但并不导入他们. 并且必须通过全名来引用。使用iex 来演示:

☁  learn-elixir  iex
Erlang/OTP 17 [erts-6.4] [source] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]

Interactive Elixir (1.0.4) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> require Geometry
nil
iex(2)> whoIam?
** (RuntimeError) undefined function: whoIam?/0

iex(2)> Geometry.whoIam?
Elixir.Geometry
:ok
iex(3)>

require 相对 import的差别在于并没有真正的导入模块,而是在编译链接的时候,查找对应的共享库。要使用其他模块所提供的函数和宏, 就必须要require进来。即编译加载给定的模块,因为宏展开是在编译的时候。

require和import有部分重叠的作用,前者更多的是针对宏的操作。

总结

elixir提供了四个模块指令,用于引用导入模块的函数或宏。import 和 alias比较相近,都是导入模块的函数,其中import可以导入指定的函数,默认导入所有。alias则只是把模块引用名给重命名而已,调用函数的时候还需要把别名加上。use仅执行模块的using 宏的代码。require则是对模块宏在编译时加载。关于他们更多的细节,在宏的笔记之后再做介绍。

上一篇下一篇

猜你喜欢

热点阅读