Skip to content

Add warnings on function exported and optional callback #14552

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions lib/elixir/lib/kernel.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4366,9 +4366,14 @@ defmodule Kernel do
Returns `true` if `module` is loaded and contains a
public `function` with the given `arity`, otherwise `false`.

Note that this function does not load the module in case
it is not loaded. Check `Code.ensure_loaded/1` for more
information.
> ### Unloaded modules {: .warning }
>
> This function does *not* load the module in case it is not loaded
> and Elixir lazily loads modules by default (except on releases).
> This may yield unexpected results in testing when module
> usage order is random.
>
> See `Code.ensure_loaded/1` for more information.

Inlined by the compiler.

Expand Down
5 changes: 5 additions & 0 deletions lib/elixir/pages/references/typespecs.md
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,11 @@ Note you don't need to define a behaviour in order to dynamically dispatch on a

Optional callbacks are callbacks that callback modules may implement if they want to, but are not required to. Usually, behaviour modules know if they should call those callbacks based on configuration, or they check if the callbacks are defined with `function_exported?/3` or `macro_exported?/3`.

> Testing Optional Callbacks {: .warning }
>
> `mix test` may exhibit unexpected behaviour when testing a conditional call to an optional callback gated
> on `function_exported?/3`, see the documentation on this function for details.

Optional callbacks can be defined through the `@optional_callbacks` module attribute, which has to be a keyword list with function or macro name as key and arity as value. For example:

defmodule MyBehaviour do
Expand Down