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

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
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
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
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
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
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New

Other popular topics Top

skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36128 110
New
boundedvariable
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
Qqwy
Update: How to use the Blogs &amp; Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 126479 1222
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

We're in Beta

About us Mission Statement