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
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 know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
Hi everyone,
I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding.
I sta...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
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
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
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
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
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #metaprogramming
- #hex











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!