當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


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)。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。