eeff

eeff

About ambiguity introduced in function default arguments

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 ?

Marked As Solved

mudasobwa

mudasobwa

Creator of Cure

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.

Also Liked

mudasobwa

mudasobwa

Creator of Cure

Here is the documentation:

If a function with default values has multiple clauses, it is required to create a function head (a function definition without a body) for declaring defaults — Modules and functions — Elixir v1.20.2

Just don’t try to hack the proposed syntax. Supervisor also ditched this cludge, that’s why I suggested to use the latest versions of the toolchain.

odelbos

odelbos

You need to do it like this:

defmodule A do
  def f(a, b \\ [])

  def f(a, []), do: "f(#{inspect(a)})"
  def f(a, b) when is_list(b), do: "f(#{inspect(a)}, #{inspect(b)})"
end

IO.puts A.f(5)
# Output --> f(5)

IO.puts A.f(5, [:one, :two])
# Output --> f(5, [:one, :two])
odelbos

odelbos

It’s because of the declarative f/1 order.
When we write:

def f(a, b \\ [])

I fact the compiler will expand it in two functions:

def f(a), do: f(a, [])  # <-- This is not a declarative f/1 it's the default-argument of f/2
def f(a, b) do: ...

So in the first case

defmodule A do
  def f(a), do: "f(#{inspect(a)})"     # <--- declarative f/1

  def f(a, b \\ []) when is_list(b), do: ...   # <--- will be expanded by the compiler
end

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:

defmodule A do
  def f(a, b \\ []) when is_list(b), do ...   # <--- will be expanded by the compiler
  def f(a), do: "f(#{inspect(a)})"     # <--- declarative f/1
end

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.

Where Next?

Popular in Questions Top

chokchit
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2733ms. You can configure how long re...
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
Kurisu
For example for a current url like http://localhost:4000/cosmetic/products?_utf8=✓&amp;query=perfume&amp;page=2, I would like to get: ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31307 143
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New

We're in Beta

About us Mission Statement