Where is `__info__/1` documented?

落爺英雄遲暮 提交于 2020-01-16 03:53:12

问题


Found this in Ecto.Repo.Supervisor and I was wondering where the other options to __info__/1 are documented:

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

  # (...)
  end

The Module documentation only mentions that

After a module is compiled, using many of the functions in this module will raise errors, since it is out of their scope to inspect runtime data. Most of the runtime data can be inspected via the __info__/1 function attached to each compiled module.


回答1:


Found it in 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)

Undocumented atoms

:deprecated - shows deprecated functions in a module that are prefixed with the @deprecated attribute (included in 1.7.0-rc.0)


Test drive

Trying out the above behaviour = snippet above in a Phoenix project:

$ 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]


来源:https://stackoverflow.com/questions/53059471/where-is-info-1-documented

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!