Smool

Smool

I’ve got a pretty generic GenServer that I’ve created that I want to use in a few contexts. In each context, there’s a specific function that the component will have to call, but I want the specific implementation to be different for each instance. I’m still new to elixir, so I may be wrong with this, but I figure I have the following options:

  1. pass the function as an init arg to the component
  2. Make a bass component and then override the specific behaviour by subclassing via “use” in the same way that I currently ‘sub-class’ genserver via “use” (How do I properly phrase this? It feels like ‘subclassing’ but there’s probably a proper way to say this.

Also, in general, are there any problems with passing functions/closures between processes? Would it still work in a distributed Elixir environment?

Showing Posts 1 to 5

al2o3cr

al2o3cr

What you’re describing sounds like a behavior. Behaviors specify @callback functions that any module that uses the behavior needs to define. You’ll frequently see those implementations annotated with @impl true or @impl WhateverTheBehaviorModuleIs.

GenServer and Plug are both good examples of this style.

ityonemo

ityonemo

second the look up behaviours recommendation.

Elixir module/function calls are duck typed. You can do something like this:

module = SomeModuleName
module.some_function()

and there there’s no compile time checking to see if some_function is valid in the SomeModuleName module namespace.

you can pass closures between nodes. Just do it!

1.Make two sname’d elixir nodes (iex --sname foo --cookie bar; iex --sname baz --cookie bar),
2. connect them with Node.connect/1
3. on baz, do the following:

spawn(fn ->
  lambda = receive do f -> f end
  IO.puts(lambda(1))
end)
|> Process.register(Catcher)

then on foo do the following:

lambda = fn x -> x + 1 end
Node.spawn(:baz@<whatever-your-hostname-is>, fn ->
  send(Catcher, lambda)
end)

and of course, it will work. Dirty little secret is that Node.spawn/2 is just by itself sending a lambda and running it on the other side so that sending lambdas in general should work should maybe not be surprising!

If you want to go into even more “BEAM is awesome” territory, you can pickle your lambda using :erlang.term_to_binary, send it over the internet to another node, then unpickle the lambda using :erlang.binary_to_term, and it will just work™ (with some exceptions, like pids won’t be translated correctly)

rvirding

rvirding

Creator of Erlang

Sending a function from one node to another is very sensitive and needs to be done very carefully. So doing a Node.spawn/2 with a fn or sending a fn requires that the module in which the fn is defined is already loaded on the other node. It also demands that it is exactly the same version of the module without any changes and preferably the same compiled code.

The reason for this is that when a module is compiled an md5 checksum of the code is included in the module definition. Internally a function includes the name of the module in which it is defined and that modules checksum. When it is called the module name is used to find the function code and the checksums are compared, and they have to be the same.

peerreynders

peerreynders

Welcome to the forum!

That ceremony could overcomplicate things at least initially as I suspect that there already is a GenServer callback module implementation that does most of the work:

I’ve got a pretty generic GenServer that I’ve created that I want to use in a few contexts.

What it sounds like is that the GenServer callback module in itself needs further specialization by being bound on yet another separate callback module - within this discussion the strategy callback module.

So really the essence here is that a callback module is simply the name of a module that promises to implement certain functions that the behaviour module will invoke on it.

The name of the strategy callback module can be passed as one of the arguments to the init/1 callback and stored in the GenServer state, e.g.:

  def init(args) do
    with {:ok, strategy} <- Keyword.fetch(args, :strategy) do
      {:ok, %{strategy: strategy}}
    else
      _ ->
        {:stop, :badarg}
    end
  end

Then later the function can be called as (provided the GenServer state is bound to state):

result = state.strategy.the_fun(the_arg)

or

result = apply(state.strategy, :the_fun, [the_arg])

Kernel.apply/2

At the simplest level I would compare behaviours to the strategy pattern though there are also elements of the template method pattern.

Smool

Smool OP

Awesome, thank you everyone for the good insights!

— 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
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
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
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
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
apz
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
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
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
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

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews