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

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
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
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
ryanwinchester
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted” Version...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
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
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
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

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews