jakub-zawislak

jakub-zawislak

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:

Showing Posts 1 to 7

Qqwy

Qqwy

TypeCheck Core Team

Protocols are in fact a wrapper around Behaviours, that reduce a little boilerplate, but will only work if your input is a struct (or one of Elixir’s built-in datatypes). Dispatching a protocol based on a pattern-match is not possible.

Instead, I’d advise you to just write a behaviour for this.

samphilipd

samphilipd

If one of your implementations has to deal with arbitrary structs then defimpl can’t help you I think. If it’s one particular type of struct then it should be fine.

I tried to demystify protocols a bit and explain the difference between those and behaviours in my post here.

Could you post here the details of the other struct you’re trying to match on? I think you can use protocols for this.

jakub-zawislak

jakub-zawislak OP

I’m developing a form library. It now requires Ecto and I want to extract that Ecto stuff into another library. I want to write a core lib in such a way that it don’t know about existence of that new Ecto extension library.

Currently this is the only way I can create a form:
create_form(ArticleType, %Article{}, params) where Article is an Ecto schema.

I think it would be nice if in a new version I could use it like that

  • create_form(ArticleType, %SomeStructNotSchema{}, params) - a normal struct, handled by core lib
  • create_form(ArticleType, %{}, params) - a map, handled by core lib
  • create_form(ArticleType, %Article{}, params) - Ecto schema, handled by new Ecto lib

but I’m afraid it’s impossible :smile:

Of course I know that I could create different functions, like Formex.Ecto.create_form, Formex.Something.create_form… but I’m curious if it is possible to avoid that.

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

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:

marcelotto

marcelotto

Very nice.

I’m thinking about using it for an Elixir implementation of SPARQL for which I hope ProtocolEx would allow me to pattern match on AST expressions and my general tree traversal functions could call protocol dispatched functions on SPARQL graph patterns and algebra operators. Do you think it is possible and advisable to try something like that with ProtocolEx? How stable do you consider it?

Any chances this can become somehow part of the language? I’ve noticed that José forked it :wink:

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). ^.^;

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
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
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews