当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Elixir Module.register_attribute用法及代码示例


Elixir语言中 Module.register_attribute 相关用法介绍如下。

用法:

register_attribute(module, attribute, options)
@spec register_attribute(module(), atom(), accumulate: boolean(), persist: boolean()) ::
  :ok

注册一个属性。

通过注册属性,开发人员可以自定义 Elixir 存储和累积属性值的方式。

选项

注册属性时,可以给出两个选项:

  • :accumulate - 对同一属性的多次调用将累积而不是覆盖前一个。新属性总是添加到累积列表的顶部。

  • :persist - 该属性将以 Erlang 抽象格式保存。在与 Erlang 库交互时很有用。

默认情况下,这两个选项都是 false

例子

defmodule MyModule do
  Module.register_attribute(__MODULE__, :custom_threshold_for_lib, accumulate: true)

  @custom_threshold_for_lib 10
  @custom_threshold_for_lib 20
  @custom_threshold_for_lib #=> [20, 10]
end

相关用法


注:本文由纯净天空筛选整理自elixir-lang.org大神的英文原创作品 Module.register_attribute(module, attribute, options)。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。