Fran

Fran

How to provide an interface to an existing Erlang behaviour

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?

First Post!

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:

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

Where Next?

Popular in Questions Top

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
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
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
makeitrein
Hey all, just started picking up Elixir last week and am writing a scraper as a learning project. Baby step #1 is extracting the number ...
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
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New

Other popular topics 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
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30048 115
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
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
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
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
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

We're in Beta

About us Mission Statement