fceruti

fceruti

Inheritance in Elixir

I’m working on a cool library (news about that in a few weeks :smiley:), and there’s no sugar coating this: I really want to use inheritance.

I’m no Elixir n00b, at least not completely. I’ve enjoyed the functional paradigm shift very much, but in this particular case, I can’t help but feel like I’m jumping all kinds of hoops just to emulate what simple inheritance stuff would fit perfectly.

Let me describe the problem, then the best solution I’ve come up with, and I hope you can help me come up with a better one.

So, in this library, there’s lots of configuration to do. Most of this configuration will be written by the author of the library, lets call them starting points, and there are many of them. Users can use one of these starting points and just modify whatever they want to change and keep the rest. A config that uses a base “template” can also be a template for another config, and they would catch these config definitions in a cascade manner.

That’s probably the longest I’ve written about inheritance without mentioning it.

Right now I’ve got something like this:

defmodule Config do
  @callback __attr_value(atom(), atom()) :: atom()
  @optional_callback __attr_value: 2

  defmacro __using__(_opts) do
     quote do
        @behaviour Config

         def attr_value(category, item) do
           try do
               __MODULE__.__attr_value(category, item)
           rescue
              _e in [UndefinedFunctionError, FunctionClauseError] ->
                variables = __MODULE__.__info__(:attributes)
    
                if parent = Keyword.get(variables, :parent) do
                  parent.__attr_value(category, item)
                else
                  default_value(category, item)
                end
           end
         end
     end 
  end
end

defmodule BaseConfig do
   use Config

   def __attr_value(:category, :name), do: :my_value
   def __attr_value(:category, :picture), do: :my_picture
end
   
defmodule ChildConfig do
   use Config
   @parent BaseConfig

   def __attr_value(:category, :name), do: :another_value
end

It works, but I don’t know, I’m feeling uneasy with it.

What do you think? Have you seen this in the wild? Is there a better way?

Marked As Solved

dimitarvp

dimitarvp

Quick shot at this: why not use defdelegate in the derived (child) configs?

Also Liked

cmo

cmo

I presume you mean something more advanced than a module attribute?

defmodule ChildConfig do
   @behaviour MyConfig
   @delegate BaseConfig
   def attr_value(:category, :name), do: :child_name
   defdelegate attr_value(a, b), to: @delegate
end
fceruti

fceruti

Oh man, thanks this is awesome. I got lost in a sea of macros when the solution was so simple :slight_smile:

Thanks!

eksperimental

eksperimental

First, do not use Config as the name for your behaviour since it is a module in Elixir.

Then I would work with module attributes and do compile time checks.
What i usually do is create functions in the behaviour module that are called by default with the default implementations defined in __using__/1. Also look into how to play with òptions passed to this macro.
Then also make use of defoverridable/1 to let the user rewrite the default implementation.
You should be covered by all this.

Last Post!

krasenyp

krasenyp

This is what I’ve implemented numerous times and works well. Something similar is shown in the article Macro Madness: How to use `use` well - DockYard.

Where Next?

Popular in Questions Top

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
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
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
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

Other popular topics Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
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
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New

We're in Beta

About us Mission Statement