Skip to content

Protocol functions with default arguments produces unclear error #15071

@kipcole9

Description

@kipcole9

Elixir and Erlang/OTP versions

elixir 1.20.0-rc.1-otp-28
erlang 28.3.1

Operating system

MacOS

Current behavior

Up to Elixir 1.19, the following protocol definition does not error. In Elixir 1.20 it produces an error which is quite tricky to work out.

defprotocol TestProtocol do
  def to_string(name, options \\ [])
end

The error is:

    warning: protocols can only define functions without implementation via def/1, found: to_string/11defprotocol TestProtocol do~~~~~~~~~~~~~~~~~~~~~~~~~~~
    │
    └─ lib/prototest_protocol.ex:1: TestProtocol (module)

It took me longer than expected to realise this is because the handling of default parameters produces multiple function clauses with a delegate implementation.

Expected behavior

  1. Restore the behaviour of Elixir 1.19 or
  2. Improve the error message when protocols attempt to define callbacks with default arguments.

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