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


Elixir Module.has_attribute?用法及代码示例


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

用法:

has_attribute?(module, key)
(从 1.10.0 开始)
@spec has_attribute?(module(), atom()) :: boolean()

检查给定属性是否已定义。

如果属性已使用 register_attribute/3 注册或分配了值,则该属性被定义。如果使用 delete_attribute/2 删除了某个属性,则不再认为该属性已定义。

该函数只能在尚未编译的模块上使用。

例子

defmodule MyModule do
  @value 1
  Module.register_attribute(__MODULE__, :other_value)
  Module.put_attribute(__MODULE__, :another_value, 1)

  Module.has_attribute?(__MODULE__, :value) #=> true
  Module.has_attribute?(__MODULE__, :other_value) #=> true
  Module.has_attribute?(__MODULE__, :another_value) #=> true

  Module.has_attribute?(__MODULE__, :undefined) #=> false

  Module.delete_attribute(__MODULE__, :value)
  Module.has_attribute?(__MODULE__, :value) #=> false
end

相关用法


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