juanseph

juanseph

OOP like handler inheritance

I have been doing functional for a while already, but there’s some OOP pattern that I still like and can’t find the right approach to it

This pattern is a hierarchy of objects, a set of handlers (per object type) and a factory to instantiate the right handler based on caracteristics of the object.

I was wondering if someone has a nice and elegant implementation of this whole thing (behaviors with something else maybe?). I guess with pattern matching it can be cool

Some OOP example

Msg1 extends Msg extends Dao
Msg2 extends Msg extends Dao

Abstract MsgDispatcher
Abstract send(Msg m)

DispatcherOne extend MsgDispatcher
send(Msg m): ...

DispatcherTwo extend MsgDispatcher
send(Msg m): ...

Factory.getDispatcher(Msg1).send(Msg1) will invoke the method in DispatcherOne

Thanks!

Most Liked

kokolegorille

kokolegorille

No inheritance, rather composition.

That’s a good fit for Protocol.

Pattern matching? Multi clause functions should do the job, maybe.

ityonemo

ityonemo

the official way to do this is with behaviours protocols, and a liberal use of defdelegate.

If you don’t want to do it with protocols directly, you can also use a protocol-lite:

defmodule Api do
  @callback foo(struct) :: integer
  @callback bar(struct) :: integer 
end

defmodule Msg1 do 
  defstruct [... fields ...]

  @behaviour Api

  def foo(struct), do: ...
  def bar(struct), do: ...
end

defmodule Msg2 do
  # demonstrates oop-style multiple-dispatch
  defstruct [... fields ...]

  @behaviour Api

  def foo(struct), do: ...
  def bar(struct), do: ...
end

defmodule Msg3 do
  # demonstrates OOP-style inheritance
  defstruct [... fields ...]
  
  @behaviour Api

  # note that the fields in Msg3 type must be compatible with the Msg1 and Msg2
  # function expectations of the field parameters, or else you will have a bad time.
  defdelegate foo(struct), to: Msg1
  defdelegate bar(struct), to: Msg2
end

defmodule Action do
  def adds_foo_and_bar(msg = %msg_type{}) do
    msg_type.foo(msg) + msg_type.bar(msg)
  end
end

# these statements dispatch against their respective module's foo and bar statements
Action.adds_foo_and_bar(%Msg1{...})
Action.adds_foo_and_bar(%Msg2{...})
Action.adds_foo_and_bar(%Msg3{...})

Note that making sure that the structs have compatible fields you need is your responsibility, if you want you can probably whip up some compile time macros to check that the structs are compliant**. And the “inheritance” of your functional components is also very much explicit, there’s no “inherit” that makes your code consumer guess what gets inherited and what doesn’t.

** hints: A module with defstruct implements the __struct__/0 function and at compile time you can retrieve the module’s struct-being built with Module.get_attribute(__MODULE__, :struct)

Anyways this kind of stuff is a little bit on the hard side because maybe this sort of abstraction is not necessarily the best thing to do. I’m still not sure if it’s a good pattern from OOP or an antipattern.

Where Next?

Popular in Discussions Top

PragTob
Hello everyone, I know we had quite some threads (read through lots of them) about background job processing but it remains a hotly deba...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 53578 245
New
Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
pillaiindu
In django there is a cache framework backed by memcached. Rails also puts a lot of emphasis on caching, and even the idea of russian-doll...
New
MarioFlach
Hello, I want to share a project I’ve been working on for a while: Background Some time ago I came across a talk: How we scaled git l...
New
pillaiindu
I want to convert a Phoenix LiveView CRUD website to a CRUD mobile app. What do you think is the easiest way to do so?
New
crispinb
On reading dhh’s latest The One Person Framework it strikes me that Phoenix with LiveView is already pretty much this. However, never hav...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39247 209
New
IVR
Hi all, I’ve seen a number of related threads in the past, but I’d still be very curious to hear an up-to-date opinion on this topic. I...
New
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
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
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
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
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
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31107 143
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
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
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

We're in Beta

About us Mission Statement