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

ricklove
I was just introduced to Elixir and Phoenix. I was told about the 2 million websocket test that was done 2 years ago. From my research, t...
New
jer
I’ve been using umbrellas for a while, and generally started off (on greenfield projects at least) by isolating subapps based on clearly ...
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
nburkley
AWS re:Invent is on at the moment with some interesting announcements. One new feature in particular is the Lambda Runtime API for AWS La...
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
mmmrrr
Just saw that dhh announced https://hotwire.dev/ Is it just me or is this essentially live view? :smiley: Although I like the “iFrame-e...
New
AstonJ
Can you believe the first professionally published Elixir book was published just 8 years ago? Since then I think we’ve seen more books f...
New

Other popular topics 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
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
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
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
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

We're in Beta

About us Mission Statement