Adzz

Adzz

Solving the Expression Problem with Elixir

Hello,

I have been thinking about how Elixir might help us solve the expression problem. There are some really good articles on how clojure does it with protocols, so I thought I’d give it a go:

https://tech.nested.com/solving-the-expression-problem-with-elixir-916bb9b5dd74

The solution I came to is very un-idiomatic elixir. I’d be interested in hearing how others would approach the problem!

Most Liked

OvermindDL1

OvermindDL1

Not heard of it referred to as the ‘Expression Problem’, but that is a good example as to why both pure functional and OOP are very bad at solving it, protocols are not really better than OOP here, what would work best is multiple polymorphic dispatch (rehashing a bit of the article to set up context here), like CLOS (Lisps’s OOP system) supports, where you can dynamically dispatch on more than one argument, which is easily solved via pattern matching in Elixir (but requires horrible dispatch trees and such for OOP). Here’s an example (typed in post so probably errors somewhere) using my ProtocolEx library (which builds matchers at compile-time, think of it as a significantly more powerful version of the built-in Protocols):

import ProtocolEx
defprotocol_ex Overlap, as: shapes do
  def overlap?(shape, other_shape), do: overlap?({shape, other_shape})
  def overlap?(shapes)
end


defimpl_ex SquareRect, {%schemal{}, %schemar{}} when schemal in [Square, Rect] and schemar in [Square, Rect], for: Overlaps do
  def overlap?({%Square{}, %Square{}}), do: :test_if_squares_overlap
  def overlap?({%Rect{}, %Rect{}}), do: :test_if_rects_overlap
  def overlap?({%Rect{}, %Square{}}), do: :test_if_rect_overlaps_square
  def overlap?({%Square{}, %Rect{}}), do: :test_if_square_overlaps_rect
end

defimpl_ex CircleRect, {%schemal{}, %schemar{}} when schemal in [Circle, Rect] and schemar in [Circle, Rect], for: Overlaps do
  def overlap?({%Circle{}, %Circle{}}), do: :test_if_circles_overlap
  def overlap?({%Rect{}, %Circle{}}), do: :test_if_rect_overlaps_circle
  def overlap?({%Circle{}, %Rect{}}), do: :test_if_circle_overlaps_rect
end

Or whatever, there’s many ways of doing it, and you can split each case of overlap into it’s own module, or put them all together, or define defaults, or control ordering, or let dependencies of this add in their own things for their own types, or use tagged tuples instead of structs, or whatever. :slight_smile:

Shikada

Shikada

I’m not sure if you watched this talk already, but it’s very relevant to what you’re doing.

Adzz

Adzz

No I haven’t, it looks good. I understand protocols well already though. Just interested to hear other’s thoughts

Last Post!

OvermindDL1

OvermindDL1

Yes, hence why I was speaking of my ProtocolEx library, which is like Elixir’s Protocols except it works based on matchers thus you can actually multi-dispatch instead of single-dispatch. :wink:

Where Next?

Popular in Discussions Top

AstonJ
If a newbie asked you about Phoenix Contexts, how would you explain the basics to them? Feel free to be as concise or in-depth as you li...
New
sergio
There’s a new TIOBE index report that came out that shows Elixir is still not in the top 50 used languages. It also goes on to call Elix...
New
rms.mrcs
A couple of days ago I was discussing with a friend about different approaches to write microservices. He said that if he was going to w...
New
tomekowal
Hey guys! I want to create a toy project that shows a chart of temperature over time and updates every 5 seconds. I feel LiveView is per...
New
Fl4m3Ph03n1x
Background This question comes mainly from my ignorance. Today is Black Friday, one of my favorite days of the year to buy books. One boo...
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New
Rustixir
Hi everyone, im working on find best language/framework/system for high concurrency, high performance and stable performance after wor...
New

Other popular topics Top

jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
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 31525 112
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 40082 209
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