Adzz

Adzz

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!

Showing Posts 1 to 5

Shikada

Shikada

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

Adzz

Adzz OP

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

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:

Adzz

Adzz OP

The example in the article I wrote is achieving polymorphic dispatch using protocols, so protocols are much better than OOP for solving this specific problem.

Pattern matching in elixir doesn’t get you everything you need to solve the expression problem. You also need to be able to extend the patterns that you can match on, dynamically. Meaning the patterns you match on need to be extensible. The only way you can achieve this is with protocols AFAIKT

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:

— All posts loaded —

Where Next? Top

Trending in Discussions Top

AstonJ
As the title says, please share what you’ve been up to with Elixir. Whether that’s been learning it, looking into it, making stuff with i...
2977 91898 914
New
AstonJ
The obligatory hello world thread! Who are you and where are you from? :stuck_out_tongue:
4616 55835 594
New
byu
@chrismccord : I just saw the Extract AGENTS.md from Phoenix.new into phx.new generator commit to the phoenix project. My initial shotgu...
New
arcanemachine
I was working on an Ecto migration and I needed a timestamp. So, for the nth time, I looked up the different data types for timestamps, a...
New
alexslade
Fly’s CEO posted this recently - Turn And Face The Strange · The Fly Blog It says that Fly is going all-in on sprites, which is a worry ...
New
Herve37
We’re evaluating API mocking tools for OpenAPI-based projects and would love to hear what other teams are using. We’re particularly inte...
New
matt-savvy
Is there a word for the ~> symbol used in Version strings? Do you also just call it a Squiggle Arrow™ ?!
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

Latest on Elixir Forum

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews