stephen_m
Hi,
When writing the following code, elixir correctly and thankfully warns that there is a conflict:
function/macro 1:
defmacro static_translate(key, opts \\ [humanize: :true, downcase: :true]) do
... do stuff here
end
function/macro 2:
defmacro static_translate(module, key, opts \\ [humanize: :true, downcase: :true]) do
...do other stuff here
end
error
web/views/views_translation_helpers.ex:59: defmacro static_translate/3 defaults conflicts with defmacro static_translate/2
I’ve been dealing with these situations a few times and I’m never quite happy with my refactoring…
how would you rewrite this?
(please assume the function logic is sufficiently different to warrant a separate function completely)
Trending in Questions
Hey guys,
I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly
Do you guys have any suggestions what is the best prac...
New
Hello!
Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app.
I creat...
New
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
Anyone here using Honeybadger?
My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of
Bandit.HTTPError...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
Other Trending Topics
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
There are three potential reasons for members of this forum to have a look at https://vutuv.de
You are tired or annoyed of LinkedIn.
Yo...
New
Aludel - LLM Evaluation Workbench
Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixir-ls
- #ai
- #elixirconf-us
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming











Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
NobbZ
The only possible way to refactor here is to rename one of the macros/functions.
is getting expanded at compile time to the following:
As you can see, there are now 2
foo/2, which is the mentioned conflict in the error you posted.stephen_m
Thanks for the answer… This is what I did in the end.
My refactoring ended up like this…
I wasn’t sure if renaming was bad or not. In this case, it turned out better because the second one is a dynamic translation – but regardless of that if it wasn’t, I would not have wanted to rename.. but if this is the only way, then so be it.
Cheers for your input.
OvermindDL1
This is why I link to call everything via Modules. I have
alias MyServer.Permsfor everything related to Perm is called likePerms.Admin.blahand so forth. If it is a long module name I:asit to something shorter, but I always like to have names being called. It prevents these issues and lets me know ‘where’ something is that I am calling.EDIT: Or wait, I think your above functions are in the same module? Yeah that is a big conflict then.
Either make different names for them (as you did) or make a ‘header’ function that calls whichever specific it needs.
davearonson
Looks to me like you’re saying
moduleis optional… so maybe you could makemoduleone of theopts?stephen_m
in this case module is not optional but fair suggestion…
JHG
Good explanation. I get similar error but in a case a bit different. For example:
With that code everything well. But if I try to do that
cto be optional for the case where I match with a tuple of 2 elements then I get the error about defaults conflicts.But the code that, if I understood well, is equivalent to
same({a, b}, c \\ 1)works well. Could this case be an error in compiler that check the number of arguments but does not check the pattern matching in arguments?(I replied in this thread as it’s about same topic, I’m not sure if it’s better open new thread or to reply here).
peerreynders
Welcome to the forum!
could be interpreted verbatim as calling
same/2or asdue to the optional parameter on
same/3.How is the compiler supposed to know which one you mean?
Don’t mistake pattern matching for static typing.
Note:
i.e. you should have gotten an error on your first example.
Your ordering happened to create this:
The behaviour you are looking for seems to be this:
JHG
Thanks!
Then I din’t understand the explanaiton of @NobbZ because what I understood is not that
same({a, b}, c)could be interpreted as a call tosame/2orsame/3and then it check what call to do. I though what it is expanded to 2 definitions, then the previous call always will besame/2and then as the pattern matching in the argument is before of same arity without that it’ll match with the first (as it’s expected).@NobbZ said:
Then, in that example 2 functions have same arity and don’t do pattern matching in one, then I won’t can call to both; but with the pattern matching in one argument, if this is before of argument without it, I’ll can call to both. Of course, there is not so much sense to order with same arity without the tuple matching in one argument first because then nothing will match because it’s checked in order. But, if:
is getting expanded at compile time to the following:
As, if it’s correct, the expansion is in the function code, not in the call, a call as
foo({1, 2}, 3)is alwaysfoo/2then it must match with:And because the order is correct, with the argument that match the tuple before, then it’ll match with first of
foo/2. Then my second example in first post:if expands to:
Must works (and it works if I write as it’s expanded), then if compiler can’t expand a code that’s correct when it’s write as expanded, isn’t it an error? I understand that if it only check arity then try to avoid error, but when it as matchings in arguments it could be correct and works. Only problem could be if it doesn’t expand the function definition and it expand functions calls, that I didn’t understood that way.
Yes, I used 1, 2 &3 only to show in console where it did the call, but that’s the correct behaviour. What I tried to tell is what that could be written as:
And expanded to:
But:
When the expected expand will work well and won’t raise errors compiling the expanded version.
Then, maybe compiler could not to raise the error when even arity is same the arguments have something that can match, specially when the order is correct (if order is not good compiler show a warning), not to force to write the expanded version when the idea of
\\, I think, is not to write the expanded version, to do same with less, compiler is not avoiding error, is raising an error in a code that expanded will work well.EDIT: Or I’m ignoring something I don’t know/understood about expansion and the compiler and it makes sense not to do that expansion and avoid it to avoid errors.
peerreynders
That is actually
which is equivalent to
Optional parameters are not a feature of the underlying language - Erlang. It’s something that Elixir layers on top in a way that “works most of the time as expected”. But there can be surprising edge cases.
This creates ONE function (
same/2):This creates TWO functions (
same/2andsame/3):Taking that into account
The error probably relates to the fact that two separate clauses with optional function parameters emit code for the same function:
same/2.The intent that is behind
cannot be implemented with optional parameters but can be implemented long-hand
JHG
Thanks @peerreynders I think I get it now with:
I expected Elixir could handle it. I guess I found one of that surprising edge cases.
I don’t know if to enhance optional parameters is in the roadmap (in the end, it’s not important as it can be handle long-hand) but it’s nice, hopeful it’ll have less edge cases over time.
Thank you so much for the explanation!