JKWA
Author of Advanced Functional Programming with Elixir
I didn’t have room for this in my book, Advanced Functional Programming with Elixir , but I still thought it was worth sharing on my blog.
Trending in Blog Posts
The code in this post is Erlang, but it all applies to Elixir binaries just the same: how appends, match contexts and sub-binaries behave...
New
eMudhra’s leaked CA keys reached my Phoenix app through 2 trust stores, and my first redeploy patched neither. How I fixed the OS bundle ...
New
A deploy killed my OTP processes but not the containers running my AI agents. That mismatch kept a kanban card “executing” for eighteen h...
New
My first real blog post is up! Got five minutes? Come break some stuff. I put five demos in it, and they’re meant to be played with. Kill...
New
I run a kanban of AI coding agents, and one afternoon the board showed three tasks running while the profile page on the same screen said...
New
I wrote a post on how to implement a feature flag system and A/B testing in an Elixir app with no need for third-party services or librar...
New
Following up on Making `dns_erlang` fast: binary tricks for the BEAM, there’s our second post in our series on making erldns, an authorit...
New
Other Trending Topics
Hi there
Hopefully the right place to ask :slight_smile:
The PR is trivial, but I just cannot manage to compile or test in my fork :fro...
New
Hey there,
I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first.
...
New
Hi everyone :waving_hand:
I wanted to share something I’ve been building — NowBlind, a random chat app designed to help people discover ...
New
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly)
AI dream has many faces, but general...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #code-sync
- #discussion
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #ai
- #umbrella
- #elixirconf-us
- #full-time-contract
- #podcasts-by-brainlid
- #blog-post
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #elixirconf-eu
- #processes
- #security
- #api
- #forms
- #performance










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
adamu
These two statements contradict each other, perhaps the footer on the blog is generic and doesn’t apply in this case?
I think behaviours also deserve a mention along with protocols. My experience is that it’s quite rare to write a protocol, compared to a behaviour.
For example,
Eqcould also be implemented using a behaviour:krasenyp
I really don’t know why there’s this behaviour fetish in the community. Protocols are for polymorphism. You use the protocol more or less directly. With behaviours you fill holes by providing callbacks. There is a framework, like the GenServer, which calls the module which implements the callbacks.
LostKobrakai
I’m not sure I see the point in a polymorphic
equal?in the first place. One of the usecases for (dynamic) polymorphism is that the interface can exist independently from the implementations and implementations can independently maintained. Comparingatobis not a problem well suited to that if any ofaorbare of arbitrary type, because both types need to interact to be compared, hence they’re no longer independent.Elixir actually kinda has a polymophic option in that domain:
Enum.sortcan receive a module to do the comparison between items in order to sort elements of a list. It’s not officially a behaviour, but uses the single@callback compare(a, b) :: :lt | :eq | :gt. The big difference on that one – it’s not polymorphism on a per type level. Each passed module needs to be able to deal with all elements it gets thrown at for comparison and you won’t be able to later bring in a new struct and make it be handled differently.JKWA
Good point on the contradictory phrasing, I trimmed these kinds of language-specific details from the book after some early feedback. The footer is just a standard note pointing readers toward the book if they want more depth, though it does not always apply perfectly.
I agree behaviours are important, but I would not say they implement polymorphism. They enforce a contract, but you still have to choose which module to call. To me, polymorphism means a function changes behavior based on the type of input, which is the protocol.
sodapopcan
I’ve found myself deriving protocols a lot to sort of mimick Rails’ “concerns” (but in a functional way) and I’m wondering if it is the optimal way.
This is a simple example that for associating a note with any other record. I do add a direct fkey to the notes table for everything it can be assoicated with (avoiding the
noteable_typeandnoteable_idpattern from other frameworks) but otherwise this mainly DRYs up having to mention these anywhere else (pattern matching to dispatch and typespecs mainly).Here’s a stripped-down example:
I wanted to key the sample small but there is also a
assoc_keyon the protocol which allow finding a note so we do:Again, I’ve been wondering about the general utility of this. We have a few of these things that all operate similarly so the abstraction feels worth it, but I’ve been wondering if I’m overlooking a simpler way (that is, outside of not using any abstraction at all).
Another I’ve seen stuff like this done in the past would be using macros to add
list_notesto all contexts that have notes. I’ve found this can cause some nasty compile time deps and still requires setting up the schema stuff.Anyway, I hope this is on topic
JKWA
Yes, we’re both solving the same problem: defining a default comparison for a type.
Here’s how I might implement the homogeneous
sort:This lets the caller sort a list using the domain’s default ordering or pass in a different
Ordwhen needed. It supports cases where the same type needs multiple comparison strategies. It also allowsOrdcomposition, making it easy to build more complex ordering logic.JKWA
I admit, I’m having a bit of trouble giving helpful advice on this one.
Your goal, as I understand it, is to reduce Ecto boilerplate, and you’re also, in a sense, extending Ecto’s macro system. Within your project, I don’t see a problem, except that it introduces a bit of indirection that you’ll need to make sure your colleagues understand.
From the perspective of protocols, what hung me up is that I was looking for the polymorphism problem you were solving, but in the end, I think you’re using the protocol more as a contract.
Is this generalizable? I see some tight coupling to your domain, so my answer would be probably not, but I might be misunderstanding.
Is there another way to solve it? Sure. But you’re approaching it from a Ruby context, and I don’t have enough Ruby experience to say whether there’s a better alternative through that lens.
dimitarvp
That’s basically why I don’t want to use protocols and I am resisting them in work places. To me they are kind of implicit. A behaviour is an explicit function argument. I am tired of magic.
JKWA
Oh, interesting. You’re seeing magic where I’m seeing decoupling. I suppose it comes down to perspective.
sodapopcan
Thanks for your reply!
Ruby’s “concern” concept is no more than a wrapper around mixins/traits, so it’s not really a Ruby specific thing but a general OO one. I guess I’ve never felt super confident of how to tackle things in Ecto that I would have used mixins/traits for in OO. Protocols always seemed the best fit. It essentially creates the classic “basic” difference between method and function calls:
and
Yes that’s really all it is. My thing is actually really an even more general abstraction around dynamically resolving an Ecto association. We have a bunch of these things and it ends up with a bunch of copy/paste code or being implemented slightly differently, so to me learning a tiny bit of reflection once is better than the inevitable inconsistencies that come about. YMMV, of course.