Skip to content

Commit 25b5898

Browse files
Document behaviour_info/1 (#13738)
Co-authored-by: José Valim <[email protected]>
1 parent eb6231d commit 25b5898

File tree

2 files changed

+76
-2
lines changed

2 files changed

+76
-2
lines changed

lib/elixir/lib/behaviour.ex

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ defmodule Behaviour do
99
attributes.
1010
1111
Instead of `MyModule.__behaviour__(:callbacks)`,
12-
`MyModule.behaviour_info(:callbacks)` can be used.
12+
`MyModule.behaviour_info(:callbacks)` can be used. `behaviour_info/1`
13+
is documented in `Module`.
1314
"""
1415

1516
@moduledoc deprecated: "Use @callback and @macrocallback attributes instead"

lib/elixir/lib/module.ex

+74-1
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,8 @@ defmodule Module do
441441
* `@typep` - defines a private type to be used in `@spec`
442442
* `@opaque` - defines an opaque type to be used in `@spec`
443443
* `@spec` - provides a specification for a function
444-
* `@callback` - provides a specification for a behaviour callback
444+
* `@callback` - provides a specification for a behaviour callback (and generates
445+
a `behaviour_info/1` function in the module, see below)
445446
* `@macrocallback` - provides a specification for a macro behaviour callback
446447
* `@optional_callbacks` - specifies which behaviour callbacks and macro
447448
behaviour callbacks are optional
@@ -590,6 +591,78 @@ defmodule Module do
590591
`@compile {:no_warn_undefined, {Mod, fun, arity}}` - does not warn if
591592
the given module or the given `Mod.fun/arity` are not defined
592593
594+
## Generated functions
595+
596+
Sometimes the compiler will generate public functions within modules. These
597+
are documented below.
598+
599+
### `behaviour_info/1`
600+
601+
This function is generated for modules that define a behaviour, that is,
602+
that have one or more `@callback` definitions. The signature for this function,
603+
expressed as a spec, is:
604+
605+
@spec behaviour_info(:callbacks) :: [function_info]
606+
when function_info: {function_name :: atom(), arity :: non_neg_integer()}
607+
608+
@spec behaviour_info(:optional_callbacks) :: [function_info]
609+
when function_info: {function_name :: atom(), arity :: non_neg_integer()}
610+
611+
`behaviour_info(:callbacks)` includes optional callbacks.
612+
613+
For example:
614+
615+
iex> Enum.sort(GenServer.behaviour_info(:callbacks))
616+
[
617+
code_change: 3,
618+
format_status: 1,
619+
format_status: 2,
620+
handle_call: 3,
621+
handle_cast: 2,
622+
handle_continue: 2,
623+
handle_info: 2,
624+
init: 1,
625+
terminate: 2
626+
]
627+
628+
### `module_info/0`
629+
630+
This function is generated for all modules. It returns all the attributes
631+
returned by `module_info/1` (see below), but as a single keyword list. See also the
632+
[Erlang documentation](https://www.erlang.org/doc/system/modules.html#module_info-0).
633+
634+
### `module_info/1`
635+
636+
This function is generated for all modules and returns
637+
information about the module. The signature for this function,
638+
expressed as a spec, is:
639+
640+
@spec module_info(:module) :: module() # Returns the module itself
641+
@spec module_info(:attributes) :: keyword()
642+
@spec module_info(:compile) :: keyword()
643+
@spec module_info(:md5) :: binary()
644+
@spec module_info(:nifs) :: module()
645+
@spec module_info(:exports) :: [function_info]
646+
when function_info: {function_name :: atom(), arity :: non_neg_integer()}
647+
@spec module_info(:functions) :: [function_info]
648+
when function_info: {function_name :: atom(), arity :: non_neg_integer()}
649+
650+
For example:
651+
652+
iex> URI.module_info(:module)
653+
URI
654+
iex> {:decode_www_form, 1} in URI.module_info(:exports)
655+
true
656+
657+
For more information about `module_info/1`, also check out the [Erlang
658+
documentation](https://www.erlang.org/doc/system/modules.html#module_info-1).
659+
660+
### `__info__/1`
661+
662+
This function is generated for all modules. It's similar to `module_info/1` but
663+
includes some additional Elixir-specific information, such as struct and macro
664+
information. For documentation, see `c:Module.__info__/1`.
665+
593666
'''
594667

595668
@type definition :: {atom, arity}

0 commit comments

Comments
 (0)