MMore

MMore

How to distribute function clauses over several files

Hello Hello,

I have a bunch of function clauses (means one function, but different clauses to match for different input parameters). This function is basically an API handler.

def handle_event_type(event_type = "car_started", _params) do
  # do sth.   
end

def handle_event_type(event_type = "car_stopped", _params) do
  # do sth. else 
end

I wanna structure and organise a little bit for better readability according to the different business use cases. So my idea was to put the different function clauses in one file per use case and import those files back into my central API handler file to consolidate everything again.

But I get the following error:

== Compilation error in file lib/project/controllers/api_controller.ex ==
** (CompileError) lib/project/controllers/api_controller.ex:90: imported Project.ApiHandler.handle_event_type/2 conflicts with local function
(elixir 1.11.4) src/elixir_locals.erl:94: :elixir_locals.“-ensure_no_import_conflict/3-lc$^0/1-0-”/2
(elixir 1.11.4) src/elixir_locals.erl:95: anonymous fn/3 in :elixir_locals.ensure_no_import_conflict/3
(stdlib 3.14.1) erl_eval.erl:680: :erl_eval.do_apply/6
(elixir 1.11.4) lib/kernel/parallel_compiler.ex:314: anonymous fn/4 in Kernel.ParallelCompiler.spawn_workers/7

Any other idea to organise everything better?

Marked As Solved

Qqwy

Qqwy

TypeCheck Core Team

The error you get, is because when importing multiple modules that define a function with the same name,
the result is not “a single function” with the clauses combined.
Instead, the result is two functions, and ambiguity between them.

Multiple function clauses

def foo(1), do: "bar"
def foo(2), do: "baz"
def foo(_other), do: "qux"

are syntactic sugar for:

def foo(val) do
  case val do
    1 -> "bar"
    2 -> "baz"
    _other -> "qux"
  end
end

As soon as a module was compiled, all function clauses end up as case-statements, and the function is “finished”.
The only way to combine function clauses from multiple modules into the same function, is by exporting them at the AST (parsed-source-code) level (i.e. using macros), and combine these AST snippets.
However, this is almost always a bad idea as it will no longer be obvious where a particular event is handled. Instead, you’ll have to crawl through all files that contain some of the clauses. As such, you haven’t really gained anything by splitting the file this way: There is no separation of concerns between files.


But there is an alternative:

A relatively common approach I’ve seen used and used myself in the past, is to have the event_type be something “splittable” (either a list, or by e.g. pattern match on the front of the event type). For instance, by using / or : as “separator”, you might have events like customer:car:started, customer:car:stopped, billing:overdue, etc.
This then allows you to essentially create some event routing logic, spread out over multiple modules:

defmodule MainEventHandler do
  def handle_event(event_type, params) do
    dispatchable_event_type = String.split(event_type, ":")
    dispatch_event(dispatchable_event_type, params)
  end
  
  def dispatch_event(["customer" | rest], params), do: Customer.EventHandler.dispatch_event(rest, params)
  def dispatch_event(["billing" | rest], params), do: Billing.EventHandler.dispatch_event(rest, params)
  # etc.
end

defmodule Customer.EventHandler do
   def dispatch_event(["car" | rest], params), do: Customer.Car.EventHandler.dispatch_event(rest, params)
   # ... and add others here.
end

defmodule Customer.Car.EventHandler do
  def dispatch_event("started", params) do
    # ...
  end

  def dispatch_event("stopped", params) do
    # ...
  end
  # ... etc.
end

defmodule Billing.EventHandler do
  def dispatch_event(["overdue"], params) do
    # ...
  end
  # ... etc.
end

Also Liked

dimitarvp

dimitarvp

Generally: just don’t, you can’t achieve it the way you aimed at for the reasons @Qqwy explained excellently.

But if you really want to have some sort of routing, you might be better off just making one central router module which just delegates to a number of other modules.

Protocols I would advise against; you can make do without them for sure.

Finally, you can use behaviours and runtime hackery to dispatch to a compliant one but IMO that’s an overkill as well.

IMO just go for a router module that dispatches function calls to other modules. Standardize those modules somehow. Easiest way would be to just clump them up in one directory but if you really want to be able to find them at runtime, you can also make them implement a behaviour.

Last Post!

dimitarvp

dimitarvp

Generally: just don’t, you can’t achieve it the way you aimed at for the reasons @Qqwy explained excellently.

But if you really want to have some sort of routing, you might be better off just making one central router module which just delegates to a number of other modules.

Protocols I would advise against; you can make do without them for sure.

Finally, you can use behaviours and runtime hackery to dispatch to a compliant one but IMO that’s an overkill as well.

IMO just go for a router module that dispatches function calls to other modules. Standardize those modules somehow. Easiest way would be to just clump them up in one directory but if you really want to be able to find them at runtime, you can also make them implement a behaviour.

Where Next?

Popular in Questions Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

Other popular topics Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 44532 311
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
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
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

We're in Beta

About us Mission Statement