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’m aware of 2 such projects, are there others?
git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
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
Hi everyone.
My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
Hello folks!
So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
How Can I Optimise Compile Time Dependencies
I have been building an elixir application for about 2 years now. Many modules and files ha...
New
Other Trending Topics
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
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with.
But I don’t use it anymore.
Serverless was the promis...
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
- #ai
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #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!