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:

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

Where Next?

Popular in Questions Top

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
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
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> somethi...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: The documentation above suggests that while ...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New

Other popular topics Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29603 241
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36352 110
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 48342 226
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement