eeff
Hi, I am learning Elixir and recently reading the Supervisor source code, and came accross start_link/2 and start_link/3:
# https://github.com/elixir-lang/elixir/blob/v1.20.1/lib/elixir/lib/supervisor.ex#L733
def start_link(children, options) when is_list(children) do
# https://github.com/elixir-lang/elixir/blob/v1.20.1/lib/elixir/lib/supervisor.ex#L976
def start_link(module, init_arg, options \\ []) when is_list(options)
As far as I understand, the default argument in def start_link(module, init_arg, options \\ []) should cause another start_link/2 to be defined and should result in ambiguity ? Does guard helps the compiler reduce ambiguity and thus the compiler treats it as a second clause for start_link/2 ? If so, the two clauses does not need to be grouped together ?
I tried the following:
defmodule A do
def f(a), do: "f(#{inspect(a)})"
def f(a, b \\ []) when is_list(b), do: "f(#{inspect(a)}, #{inspect(b)})"
end
and result in a warning:
➜ /tmp iex test.ex
Erlang/OTP 27 [erts-15.2] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] [jit:ns]
warning: this clause for f/1 cannot match because a previous clause at line 2 always matches
│
4 │ def f(a, b \\ []) when is_list(b), do: "f(#{inspect(a)}, #{inspect(b)})"
│ ~
│
└─ test.ex:4:7
Interactive Elixir (1.19.0-dev) - press Ctrl+C to exit (type h() ENTER for help)
But if I changed the order of the def’s:
defmodule A do
def f(a, b \\ []) when is_list(b), do: "f(#{inspect(a)}, #{inspect(b)})"
def f(a), do: "f(#{inspect(a)})"
end
it results in an error:
➜ /tmp iex test.ex
Erlang/OTP 27 [erts-15.2] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] [jit:ns]
error: def f/1 conflicts with defaults from f/2
│
4 │ def f(a), do: "f(#{inspect(a)})"
│ ^
│
└─ test.ex:4:7: A.f/1
** (CompileError) test.ex: cannot compile module A (errors have been logged)
test.ex:4: (module)
Can anybody point me to related documentation about this behavior difference ?
Trending in Questions
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
Hello,
I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind.
However, when I launch mix phx.server, I get an error...
New
Hi everyone.
My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
Hello folks!
So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
I’m working on a small exercise involving update_in/3, and I came up with this solution:
data = %{
name: "Periodic Table",
category:...
New
How Can I Optimise Compile Time Dependencies
I have been building an elixir application for about 2 years now. Many modules and files ha...
New
Other Trending Topics
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
A little off-topic, but I feel like people here have a good head on their shoulders.
I used to be quite good at making software. Was luc...
New
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #ai
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #security
- #metaprogramming










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
hauleth
The difference is that in
Supervisorexample you have provided there is guard, which limits about of cases it will match.So the expanded code will look like
So as you can see, there is a situation when 1st clause match.
If you swap order of these functions, then it will mean that there would be no option to call first clause.
eeff
I still don’t understand why switching the order would switch between warning and error.
mudasobwa
Sidenote: Please upgrade the toolchain to Elixir1.20.1 / OTP29.
The compiler explicitly handles
\\operator because Erlang does not have default argument values. It does not allow any redeclaration after\\has been expanded. Look-behind, though, would have a ton of redundant tracking involved, decreasing performance, that’s why it does not blow up immediately.The error you observe is raised by first-pass compiler, meeting the
\\default parameters within the function redeclaration.The warning you observe if you swap clauses is the second-pass compiler, simply warning you about unreachable clause, it has nothing to do with
\\because there is no look-behind.odelbos
You need to do it like this:
eeff
yes, I know about this but this does not answer my question. thank you anyway!
eeff
this does make more sense to me, I guess there are no public documentation about this behavior.
mudasobwa
Here is the documentation:
Just don’t try to hack the proposed syntax.
Supervisoralso ditched this cludge, that’s why I suggested to use the latest versions of the toolchain.odelbos
It’s because of the declarative f/1 order.
When we write:
I fact the compiler will expand it in two functions:
So in the first case
You will have two f/1 but with the declarative f/1 in first position (top-bottom) will match all clauses. So the compiler emit a warning for the unreachable expanded f/1 that handle the default-argument case of f/2.
In the second case:
The declarative f/1 is after the expanded f/1 but from the compiler’s perspective, the expanded f/1 is not a declarative f/1 function it’s the mechanism that handle the default-argument of the f/2 function. So when later the compiler encounter the declarative f/1 which is in conflict with the auto-generated f/1 it emit a compilation error.
mudasobwa
What’s “declarative function” and what does mean “in conflict” in regard to function clauses?
odelbos
A declarative function is a function written by the coder in contrast with the auto-generated (or meta-programming) function written by the compiler.
In the first case, there isn’t any conflict with the declarative f/1 (meaning the coder want this function), so when calling f/1 the declarative f/1 will be called. The compiler emit a warning saying “hey I’m doing a trick to handle the default-argument case of f/2 but it will not work” because of the declarative f/1 that will match all clauses about f/1).
In the second case, the compiler is doing his trick to handle the default-argument of f/2, so it add a f/1 but this auto-generated f/1 break the intent of the coder who declared his own f/1. This f/1 will never be called because of the auto-generated f/1, so in this case there is a conflict (breaking coder intent). This is why the compiler emit an error (“I’m doing something that break your intent”):
PS: