rschooley
This works, but I’d like to clean it up a bit (extra functions in caller):
defmodule whatever do
defp do_thing(%{source: "foo"}) do
IO.puts("calling foo")
end
defp do_thing(%{source: "bar"}) do
IO.puts("calling bar")
end
def handle_info(:dont_care, state) do
SomeContext.some_query
|> Enum.each(fun record ->
{:ok, yeah} = do_thing(record)
end)
end
end
But moving the extra private functions into their actual modules results in this error:
function do_thing/1 imported from both Bar and Foo, call is ambiguous
defmodule Foo do
def do_thing(%{source: "foo"}) do
IO.puts("calling foo")
end
end
defmodule Bar do
def do_thing(%{source: "bar"}) do
IO.puts("calling bar")
end
end
defmodule whatever do
import Foo, only: [do_thing: 1]
import Bar, only: [do_thing: 1]
....
end
I looked at protocols but didn’t see how they could clean this up. I’d like to just call a modules’ do_thing method without having a wrapper method for each one. Is there a way to do this? Thanks.
Trending in Questions
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
I’m aware of 2 such projects, are there others?
git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
Hi all,
I’m working on a Phoenix project where I need to make external HTTP requests and I’m trying to choose between Req and HTTPoison....
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
Hi,
we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New
Hi all, I’m new here and would like to hear peoples views on my Elixir/Phoenix based game-engine! :smiley:
Mutonex gamedesign was finish...
New
Hello
I’ve been coding in Elixir for almost two years now because of my current job where we implemented Elixir in our application. But ...
New
Other Trending Topics
Hey there,
I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first.
...
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
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
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself.
My main conc...
New
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly)
AI dream has many faces, but general...
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
- #blog-post
- #ecto-query
- #elixirconf-us
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #security
- #hex










Showing Posts 1 to 6- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
BrightEyesDavid
I think you can simply include the module when calling the function with
Foo.do_thing(), and that importing or aliasing a module is “just” a way to avoid having to use the module name for every call.Edit: see here and here for fuller and more accurate information.
rschooley
Hi @BrightEyesDavid,
Explicitly calling
Foo.is what I am trying to avoid.To call
Foo.I’d have to match onsourcein a case statement, or pattern match in thedo_thingfunction args (1 for each source type).I am doing the latter above, but am wondering if there is a way to just call either modules function directly based on the pattern in the struct.
To summarize, it looks like two functions can have the same arity and arg types
but
Edit: to give an example of what I have now, and it’s ok and works, but might be improved is this:
BrightEyesDavid
Ah, sorry.
I did wonder if I was missing something.
hugolnx
Hi, @rschooley! That’s a pretty abstract question/sample hehe
But, if your
do_thingis a call that make a thing so different with each kind of record, I would guess that it structure changes a lot depending on its source. If is that the case, I think it make sense that each type of record is a struct and that exist aThingDoerprotocol, so you could easily extend this thing to be done to other record types in the future. It would be something like that:With that solution you can easily extend the
do_thingto other source structures and also have the record structures documented.PS.: In that case, your
SomeContext.some_querymust return the records as structs.PS2.: I would not recommend you to use this import overriding thing even if it worked, it seems that would be a hell of situation to navigate through modules trying to discover in which of them are the matching function hehe
rvirding
To clarify things a bit. In module
Whateveryou are only defining one functiondo_thing/1which has two clauses. While it looks 2 separate functions they are actually parts of the same function. In the modulesFooandBaryou are defining 2 different functionsdo_thing/1so now you have 3 functionsdo_thing/1in separate modules. What uniquely defines a function is the module its in, its name, and its arity (number of arguments). Importing does not change that, it just gives you a way of not having to prefix calling a function in another module with the module name. You still have 3 differentdo_thing/1in 3 modules.rschooley
Interesting… thanks for this!