Fran

Fran

Hi!
I’m trying to provide/extend an existing Erlang behaviour in a library to use it with Elixir.
Said behaviour is called :riak_core_vnode, it has some setup quirks to make it to work with Elixir, so I wanted to make a sort of a wrapper behaviour, my idea is to create a module that implements said behaviour and defines a new behaviour (let’s call it :elixir_vnode for clarity’s sake). Then, a user of this behaviour would only have to implement it and the only thing my module should be doing is call the user’s defined function.
A more concrete example would be something like this:

defmodule MyWrapper do
 @behaviour :quirky_erlang_behaviour
 @callback not_weird_function(params :: any) :: :ok
 def weird_function(params) do
   UserDefinedModule.not_weird_function
 end
end

First thing that comes to mind is to use a config option to determine which module is UserDefinedModule, something like:

config :my_lib, user_provided: UserDefinedModule

And then, weird_function would be something like:

def weird_function(params) do
  user_module = Application.get_env(:my_lib, :user_provided)
  user_module.not_weird_function
end 

Is there a cleaner, or more simple, way to do this?

Showing Posts 1 to 2

al2o3cr

al2o3cr

A behaviour is ultimately just a list of callback specs, that you tell the compiler to enforce with @behaviour so that you can pass the module along to functions that expect those callbacks (for instance, passing it to :riak_core_vnode.start_link/3)

You could look to GenServer for inspiration - it defines a __using__ that applies the @behaviour and includes some default implementations:

https://github.com/elixir-lang/elixir/blob/7e4fbe657dbf9c3e19e3d2bd6c17cc6d724b4710/lib/elixir/lib/gen_server.ex#L742-L744

But notice what it doesn’t do: put @behaviour :gen_server anywhere. The callbacks have the same specs, but they are incorporated by copying instead of by reference. That may be a special case related to GenServer, though, since changing the callback functions in :gen_server would break… many things.

If you’re aiming to implement a different set of callbacks, you could make a __using__ that adds @behaviour YourWrapper and @behaviour :riak_core_vnode and supplies implementations for all the riak_core_vnode functions. Then calling code would look like:

defmodule AnElixirVnode do
  use YourWrapper

  @impl true
  def some_callback(arg, arg, arg) do
    ...
  end
end
Fran

Fran OP

I like the __using__ solution, I’ll look into it, thanks!

— 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
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
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
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