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 7 to 1

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

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

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

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

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.

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.

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.

— 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
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
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
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
jaybe78
Hello, I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter). The diffic...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
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
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
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
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
aseigo
ICal is a library for interacting with iCalendar data. It parses iCalendars into typed Elixir structs via ICal.from_ics, and can prepare ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews