Где задокументировано `__info __ / 1`? - PullRequest
0 голосов
/ 30 октября 2018

Нашел это в Ecto.Repo.Supervisor, и мне было интересно, где задокументированы другие варианты __info__/1:

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

  # (...)
  end

В документации Module упоминается только, что

После того, как модуль скомпилирован, используются многие функции этого модуля. вызовет ошибки, так как они не могут проверять время выполнения данные. Большая часть данных времени выполнения может быть проверена с помощью __info__/1 функция прикреплена к каждому скомпилированному модулю.

1 Ответ

0 голосов
/ 30 октября 2018

Найден в источнике Module :

 @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)


Тест-драйв

Попробуйте приведенный выше фрагмент 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]
...