Skip to content

fix: remove protocol definition default args deprecated in Elixir 1.20#792

Open
rafaels88 wants to merge 1 commit into
bitwalker:mainfrom
remoteoss:fix/elixir-1.20-protocol-defaults
Open

fix: remove protocol definition default args deprecated in Elixir 1.20#792
rafaels88 wants to merge 1 commit into
bitwalker:mainfrom
remoteoss:fix/elixir-1.20-protocol-defaults

Conversation

@rafaels88

@rafaels88 rafaels88 commented Jun 17, 2026

Copy link
Copy Markdown

Default arguments in defprotocol definitions are deprecated in Elixir 1.20 and will become a hard error in a future release.

  • Remove \ :microsecond defaults from Timex.Comparable.compare/3 and diff/3
  • Remove \ :utc default from Timex.Protocol.to_datetime/2
  • Drop now-invalid 1-arity @spec for to_datetime
  • Change defdelegate compare/2 and diff/2 in Timex to explicit calls with hardcoded :microsecond default to preserve public API

The warnings:

warning: default arguments in protocol definitions is deprecated
│
84 │   def compare(a, b, granularity \\ :microsecond)
│   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
│
└─ lib/comparable/comparable.ex:84: Timex.Comparable (module)

 warning: default arguments in protocol definitions is deprecated
 │
139 │   def diff(a, b, granularity \\ :microsecond)
│   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
│
└─ lib/comparable/comparable.ex:139: Timex.Comparable (module)

warning: default arguments in protocol definitions is deprecated
│
48 │   def to_datetime(datetime, timezone \\ :utc)
│   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
│
└─ lib/protocol.ex:48: Timex.Protocol (module)

Note there's also a secondary warning that comes with each ofault silently creates a lower-arity function that protocolsaren't supposed to have:

warning: protocols can only define functions without implementation via def/1, found: compare/2, diff/2
│
 1 │ defprotocol Timex.Comparable do~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
│
└─ lib/comparable/comparable.ex:1: Timex.Comparable (module)

Summary of changes

Elixir 1.20 deprecated default arguments inside defprotocol definitions — they produce both f/n and f/n-1 as protocol variants, which will become a hard compile error in a future version. Timex 3.7.13 has three such defaults: compare/3, diff/3 in Timex.Comparable, and to_datetime/2 in Timex.Protocol.

Projects running --warnings-as-errors (a common CI setup) can't compile against this on Elixir 1.20.

This removes the three defaults from the protocol definitions and makes the 2-arity public wrappers in Timex supply the hardcoded defaults explicitly, so callers see no behaviour change.

Checklist

  • New functions have typespecs, changed functions were updated
  • Same for documentation, including moduledocs
  • Tests were added or updated to cover changes - it's not testable
  • Commits were squashed into a single coherent commit
  • Notes added to CHANGELOG file which describe changes at a high-level

Default arguments in defprotocol definitions are deprecated in Elixir 1.20
and will become a hard error in a future release.

- Remove `\ :microsecond` defaults from Timex.Comparable.compare/3 and diff/3
- Remove `\ :utc` default from Timex.Protocol.to_datetime/2
- Drop now-invalid 1-arity @SPEC for to_datetime
- Change defdelegate compare/2 and diff/2 in Timex to explicit calls
  with hardcoded :microsecond default to preserve public API
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant