jakub-zawislak

jakub-zawislak

Protocols and pattern matching?

I’m trying to write two independent libraries with the same API. I thought I would achieve it with Protocols. I have already reviewed implementations of Phoenix.HTML.FormData for Plug.Conn and for Ecto.Changeset.

One of my library epxects a simple struct as argument, and the other one an Ecto’s schema. I know how to distinguish those in a pattern matching. The problem is, the defimpl ..., for: ... expects a module as :for parameter, not the pattern.

Is there any way to solve this problem using Protocols?

The only idea I have is to wrap those arguments with another modules like this: %MyLib.Struct{data: struct}, %MyLib.Schema{data: schema}. Then to write implementation of my protocol for MyLib.Struct and MyLib.Schema. But it wouldn’t be programmer-friendly :smile:

Most Liked

marcelotto

marcelotto

Protocols can’t be dispatched by pattern matching in a native way like Qqwy already mentioned, but ProtocolEx seems to get this to work.

OvermindDL1

OvermindDL1

Perfectly suited for that.

Fairly stable now, and if you use the suggested names (I have some names that only exist for backwards compatibility that ‘could’ potentially be removed in the future, but probably will not, and it would be a major version bump anyway).

It goes for more power than Elixir traditionally prefers because it is easier to break things than the stock protocol if you do things wrong (though I have a lot of checks and newer/documented styles of doing things that handle much of those issues now).

Hah, really? This is NOT an example of good code internally. It started purely as an experiment of How easy would it be to do something like this... and it indeed up being something I actually ‘use’ and I just never refactored it (refactoring would be done in a backwards compatible way though). ^.^;

OvermindDL1

OvermindDL1

Ah this is an old thread. ^.^

But yep, that was the purpose of why I made ProtocolEx, and I had a few ideas to get some extra speed out of it like being able to @inline definitions or defmacro implementations into the protocol and such, as well as inline case-by-case default implementations, all of which is implemented, and it can run circles around the stock Protocol in speed and capabilities at this point while entirely replacing its feature set with just a couple extra tokens overall. :slight_smile:

Where Next?

Popular in Questions 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
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
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
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New

Other popular topics Top

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
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
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 54996 245
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
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

We're in Beta

About us Mission Statement