rogerweb

rogerweb

Defdelegates with same arity but different argument name for different argument types

I have a module with a function with two implementations using pattern matching:

def agent_registered(%Event{} = event) do
  ...
end

def agent_registered(agent_id) do
  ...
end

My application’s main module contains only defdelegates to a number of “implementation” modules.

Someone trying to call a function from the main module can use the auto-complete feature of iex or the IDE to see the arguments it expects, which is nice. However, in the case of that agent_registered function I can’t include two defdelegates, one for each case, as it’s the same function:

This is “wrong” (a warning is displayed for the second line):

  defdelegate agent_registered(event), to: AgentRegistered
  defdelegate agent_registered(user_id), to: AgentRegistered

This is fine, but then the argument name must cater for all the possibilities:

  defdelegate agent_registered(event_or_user_id), to: AgentRegistered

I tried adding @spec to the actual function implementation informing the possible argument types but iex and IDE keeps showing the argument name as in the defdelegate.

Have you ever had to deal with such scenario? Should I simply have different functions for different arguments instead of pattern matching in this case? Should I go for def instead of defdelegate and bring the pattern matching to the main module?

Thanks!

Most Liked

baldwindavid

baldwindavid

I think it’s fine to delegate to a function with multiple heads and to just use an argument name that is clear. The name event_or_user_id is pretty obvious. As an aside, I wonder if you might consider whether it is more explicit at the callsite to have separate function names like agent_registered and agent_registered_by_id. Sometimes it’s nice having a function that can handle either a struct or an ID. Other times it can lead to a lack of clarity of what you’re dealing with and a whole bunch of functions needing to handle both data types rather than just always sticking with one or the other.

Where Next?

Popular in Questions Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
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
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
Werner
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”: 14:57:30.512 [warn] ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
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
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
chewm
Hi guys, nice to meet you to the whole forum, I’m new here, I’m trying to configure visual studio code for elixir, right now the intellis...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New

Other popular topics Top

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
Tee
can someone please explain to me how Enum.reduce works with maps
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement