rogerweb

rogerweb

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!

Showing Posts 1 to 1

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.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
RemyXRenard
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
velrest
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
nseaSeb
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
samoloth
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
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
ryanwinchester
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted” Version...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Damirados
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
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews