Skip to content

Can't define a private on_load function #10438

@marianoguerra

Description

@marianoguerra

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:

{_, 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions