Skip to content
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
20 changes: 15 additions & 5 deletions lib/elixir/lib/module.ex
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,11 @@ defmodule Module do

A hook that will be invoked whenever the module is loaded.

Accepts the function name (as an atom) of a function in the current module.
The function must have arity 0 (no arguments) and has to return `:ok`, otherwise
the loading of the module will be aborted. For example:
Accepts the function name (as an atom) of a function in the current module or
`{function_name, 0}` tuple where `function_name` is the name of a function in
the current module. The function must have arity 0 (no arguments) and has to
return `:ok`, otherwise the loading of the module will be aborted. For
example:

defmodule MyModule do
@on_load :load_check
Expand Down Expand Up @@ -1190,8 +1192,16 @@ defmodule Module do
end
end

defp preprocess_attribute(:on_load, atom) when is_atom(atom) do
{atom, 0}
defp preprocess_attribute(:on_load, value) do
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The @on_load attribute can be either {:fun, 0} or :fun. So I think we should probably leave this clause intact and add a clause that check when it is not in the tuple format above.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh missed that, that's right. We don't document it in the docs for Module though, should we?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should, yes.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it, updated. I didn't follow your suggestion but went with a single clause where we validate the value, similar to what we do for other attributes. Let me know if you prefer your suggestion and I'll change it :)

case value do
atom when is_atom(atom) ->
{atom, 0}
{atom, 0} = tuple when is_atom(atom) ->
tuple
other ->
raise ArgumentError, "expected the @on_load attribute to be an atom or a " <>
"{atom, 0} tuple, got: #{inspect(value)}"
end
end

defp preprocess_attribute(:behaviour, atom) when is_atom(atom) do
Expand Down
9 changes: 9 additions & 0 deletions lib/elixir/test/elixir/kernel/errors_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,15 @@ defmodule Kernel.ErrorsTest do
end
end

test "@on_load attribute format" do
message = "expected the @on_load attribute to be an atom or a {atom, 0} tuple, got: \"not an atom\""
assert_raise ArgumentError, message, fn ->
defmodule BadOnLoadAttribute do
Module.put_attribute(__MODULE__, :on_load, "not an atom")
end
end
end

test "interpolation error" do
assert_compile_fail SyntaxError,
"nofile:1: \"do\" is missing terminator \"end\". unexpected token: \")\" at line 1",
Expand Down