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?
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #elixirconf-eu
- #metaprogramming
- #hex











Showing Posts 1 to 2- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
al2o3cr
A behaviour is ultimately just a list of callback specs, that you tell the compiler to enforce with
@behaviourso 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
GenServerfor inspiration - it defines a__using__that applies the@behaviourand 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_serveranywhere. 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_serverwould break… many things.If you’re aiming to implement a different set of callbacks, you could make a
__using__that adds@behaviour YourWrapperand@behaviour :riak_core_vnodeand supplies implementations for all theriak_core_vnodefunctions. Then calling code would look like:Fran
I like the
__using__solution, I’ll look into it, thanks!