DaAnalyst

DaAnalyst

Implementing an (aspect) Protocol for modules implementing another Protocol

On several occasions I’ve had this need to implement a Protocol for all modules implementing one other particular Protocol e.g.

defimpl Viewable, for: Doable do
  ..
end

One could argue to merge the two protocols into one, but in those few particular cases it is not applicable over the separation of concerns, as the protocols themselves are defined in two completely separate parts of the application and their respective implementations are more like two different behavioral aspects than just the protocols (i.e. they are not just wrapping the function calls to the structure modules but providing some additional behavior as well).

Any thoughts?

Most Liked

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

You can sort of achieve this by having the Viewable protocol fallback_to_any and then in the any clause check to see if the item provided implements Doable.

Last Post!

DaAnalyst

DaAnalyst

True, but it’s not exactly the same thing as then you also need to raise if the protocol is not implemented i.e. you’re adding some boilerplate on case by case basis. Otherwise, this can also be implemented as a macro but that’s exactly my point here. The then/2 macro used to be implemented manually by different names and by different developers until it’s become an integral part of Elixir. My point here is why not add this one as well?

Where Next?

Popular in Discussions Top

mbenatti
Following https://github.com/tbrand/which_is_the_fastest |> https://raw.githubusercontent.com/tbrand/which_is_the_fastest/master/imgs...
New
restack_oslo
Hello, Please pardon me for any faux paux. I am 46 and this is my first time on a forum of any kind. I wanted to to get answers from tho...
New
pillaiindu
In django there is a cache framework backed by memcached. Rails also puts a lot of emphasis on caching, and even the idea of russian-doll...
New
tmbb
This is a post to discuss the new Phoenix LiveView functionality. From Chris’s talk, it appears that they generate all HTML on the serve...
342 18595 126
New
mikl
I wanted to capitalize a string, and tried using String.capitalize(). That generally works well, until you try to capitalize a word like...
New
pillaiindu
I want to convert a Phoenix LiveView CRUD website to a CRUD mobile app. What do you think is the easiest way to do so?
New
shishini
I think this twitter post and youtube video didn’t get as much attention as I hoped I am still new to Elixir, so can’t really judge ...
New

Other popular topics Top

joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 49134 226
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New

We're in Beta

About us Mission Statement