__info __ / 1记录在哪里?

时间:2018-10-30 07:41:01

标签: elixir

Ecto.Repo.Supervisor中发现了这一点,我想知道__info__/1的其他选项记录在哪里:

  def compile_config(repo, opts) do
  # (...)
    behaviours =
      for {:behaviour, behaviours} <- adapter.__info__(:attributes),
          behaviour <- behaviours,
          do: behaviour

  # (...)
  end

The Module documentation仅提及

  

编译模块后,使用此模块中的许多功能   将引发错误,因为检查运行时不在其范围内   数据。大部分运行时数据都可以通过__info__/1进行检查   函数附加到每个已编译模块。

1 个答案:

答案 0 :(得分:0)

Module's source中找到它:

 @doc """
  Provides runtime information about functions and macros defined by the
  module, etc.
  Each module gets an `__info__/1` function when it's compiled. The function
  takes one of the following atoms:
    * `:functions` - keyword list of public functions along with their arities
    * `:macros` - keyword list of public macros along with their arities
    * `:module` - the module atom name
    * `:md5` - the MD5 of the module
    * `:compile` - a list with compiler metadata
    * `:attributes` - a list with all persisted attributes
  """
  def __info__(kind)

未记录的原子

:deprecated-显示模块中以@deprecated属性(包含在1.7.0-rc.0中为前缀)中不推荐使用的功能


试驾

在Phoenix项目中尝试上面的behaviour =以上代码段:

$ iex -S mix phx.server

iex(3)> Ecto.Adapters.Postgres.__info__(:attributes)
[
  vsn: [168581197275628950002173003256895919063],
  behaviour: [Ecto.Adapter],
  behaviour: [Ecto.Adapter.Migration],
  behaviour: [Ecto.Adapter.Queryable],
  behaviour: [Ecto.Adapter.Schema],
  behaviour: [Ecto.Adapter.Transaction],
  behaviour: [Ecto.Adapter.Storage],
  behaviour: [Ecto.Adapter.Structure]
]

iex(4)> for {:behaviour, behaviours} <- 
...(4)>     Ecto.Adapters.Postgres.__info__(:attributes),
...(4)>   behaviour <- behaviours, 
...(4)>   do: behaviour
[Ecto.Adapter, Ecto.Adapter.Migration, Ecto.Adapter.Queryable,
 Ecto.Adapter.Schema, Ecto.Adapter.Transaction, Ecto.Adapter.Storage,
 Ecto.Adapter.Structure]