sanderson1

sanderson1

Potential regression with pattern matching inside default argument expressions

Hello,

I’ve come across a difference in behaviour between Elixir 1.9.4 and later Elixir versions (tested with 1.10, 1.10.4, 1.11.0, 1.11.2, 1.12, 1.12.3).

Reproduction

Here is a test module showing the issue:

defmodule TestDefaultArguments do
  def test(
        arg1,
        arg2 \\ (
          default = "default"
          default
        )
      ) do
    arg1 <> "-" <> arg2
  end
end

If you run then mix run -e "TestDefaultArguments.test(\"arg1\") |> IO.puts()":

  • On Elixir 1.9.4, you see the expected output arg1-default.
  • On later Elixir versions (tested on Elixir 1.10, 1.10.4, 1.11.0, 1.11.2, 1.12, 1.12.3), you see the following error message:
** (MatchError) no match of right hand side value: "default"
    (test_gettext 0.1.0) lib/test_gettext.ex:5: TestDefaultArguments.test/1
    (stdlib 3.14.2.2) erl_eval.erl:680: :erl_eval.do_apply/6
    (stdlib 3.14.2.2) erl_eval.erl:888: :erl_eval.expr_list/6
    (stdlib 3.14.2.2) erl_eval.erl:411: :erl_eval.expr/5
    (elixir 1.10.4) lib/code.ex:341: Code.eval_string_with_error_handling/3
    (elixir 1.10.4) lib/enum.ex:783: Enum."-each/2-lists^foreach/1-0-"/2

Details

I believe it’s something to do with the pattern match default = "default" within the default argument expression.

I came across this in a situation where the default argument expression is gettext("foo"). This is a macro which compiles down to:

(
  msgid = ManagerWeb.Gettext.dpgettext_noop("default", nil, "foo")
  Gettext.dpgettext(ManagerWeb.Gettext, "default", nil, msgid, %{})
)

And I get a similar error message:

** (MatchError) no match of right hand side value: "foo"

So what I care about doing is having the gettext call in the default argument expression, but I’ve tried to distill it down to what I think is going wrong.

My Question/Request

I know it’s possible to work around this by not having the gettext macro call in the default argument expression.

However, this is code that was working with Elixir 1.9.4, so I wanted to bring it up to check whether it is an expected change in behaviour, or if it is an unexpected regression?

Many thanks,
Steve

Where Next?

Popular in Questions Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
beno
I will often find my self writing things similar to: case some_value do nil -&gt; something() "" -&gt; something() _ -&gt; someth...
New
Mooodi
Given a string, how can I get access to its character by index? Enum.at("my_string", 2) doesn't work. Or rather, not char, but a substr...
New
jc00ke
Expanding on this topic: https://forum.elixirforum.net/t/map-typespec-question/19217 Let’s say I have a map with required and optional k...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

Other popular topics Top

JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1140 51847 244
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New

We're in Beta

About us Mission Statement