Elixir语言中 Module.get_attribute
相关用法介绍如下。
用法:
get_attribute(module, key, default \\ nil)
@spec get_attribute(module(), atom(), term()) :: term()
从模块中获取给定的属性。
如果属性用 accumulate
和
标记,则始终返回一个列表。如果属性没有用Module.register_attribute/3
accumulate
标记并且没有设置为任何值,则返回nil
。
@
宏编译为对该函数的调用。例如,下面的代码:
@foo
扩展为类似于:
Module.get_attribute(__MODULE__, :foo)
该函数只能在尚未编译的模块上使用。使用
回调获取所有持久属性,或使用 Module.__info__/1
检索已编译模块中的所有文档相关属性。Code.fetch_docs/1
例子
defmodule Foo do
Module.put_attribute(__MODULE__, :value, 1)
Module.get_attribute(__MODULE__, :value) #=> 1
Module.get_attribute(__MODULE__, :value, :default) #=> 1
Module.get_attribute(__MODULE__, :not_found, :default) #=> :default
Module.register_attribute(__MODULE__, :value, accumulate: true)
Module.put_attribute(__MODULE__, :value, 1)
Module.get_attribute(__MODULE__, :value) #=> [1]
end
相关用法
- Elixir Module.defines?用法及代码示例
- Elixir Module.concat用法及代码示例
- Elixir Module.delete_attribute用法及代码示例
- Elixir Module.eval_quoted用法及代码示例
- Elixir Module.put_attribute用法及代码示例
- Elixir Module.split用法及代码示例
- Elixir Module.overridables_in用法及代码示例
- Elixir Module.definitions_in用法及代码示例
- Elixir Module.has_attribute?用法及代码示例
- Elixir Module.reserved_attributes用法及代码示例
- Elixir Module.register_attribute用法及代码示例
- Elixir Module.create用法及代码示例
- Elixir Module.safe_concat用法及代码示例
- Elixir Module.attributes_in用法及代码示例
- Elixir Module用法及代码示例
- Elixir Map.keys用法及代码示例
- Elixir Map.values用法及代码示例
- Elixir Map.update用法及代码示例
- Elixir MapSet用法及代码示例
- Elixir Map.split用法及代码示例
- Elixir MapSet.intersection用法及代码示例
- Elixir Map.put_new_lazy用法及代码示例
- Elixir Map.drop用法及代码示例
- Elixir Macro.Env.fetch_alias用法及代码示例
- Elixir Map用法及代码示例
注:本文由纯净天空筛选整理自elixir-lang.org大神的英文原创作品 Module.get_attribute(module, key, default \\ nil)。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。