-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Description
Precheck
- Do not use the issue tracker for help or support (try Elixir Forum, Stack Overflow, IRC, etc.)
- For proposing a new feature, please start a discussion on the Elixir Core mailing list: https://groups.google.com/group/elixir-lang-core
- For bugs, do a quick search and make sure the bug has not yet been reported
- Please disclose security vulnerabilities privately at elixir-security@googlegroups.com
- Finally, be nice and have fun!
Environment
- Elixir & Erlang/OTP versions (elixir --version):
Erlang/OTP 23 [erts-11.1] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [hipe]
Elixir 1.10.4 (compiled with Erlang/OTP 23)
- Operating system: Ubuntu 20.04
Current behavior
Include code samples, errors and stacktraces if appropriate.
This elixir module:
defmodule MyMod do
@on_load :on_load
defp on_load do
IO.puts "hi!"
end
end
fails to compile with error mymod.ex:1: expected @on_load function on_load/0 to be defined as "def", got "defp"
This erlang module compiles:
-module(my_mod).
-on_load(on_load/0).
on_load() ->
io:format("hi!~n").
Erlang docs about on_load say: "It is not necessary to export the function"
Expected behavior
I would expect the elixir code to compile.
The change to make it work is to add an extra clause here:
elixir/lib/elixir/src/elixir_module.erl
Line 197 in 3415f90
{_, def, _, _} -> |
matching defp
too.
It's ok to close this issue if it's a design decision for the language, but I think on_load functions are better when private since it doesn't make sense for example to load a nif by calling the on_load function manually after module load, which is one of the main use cases for on_load.