timmolderez
Hi all,
I made a tiny macro library that adds object-oriented features, such as inheritance and polymorphism, on top of structs.
Github: GitHub - timmolderez/classy-structs: Adds object-oriented features, such as inheritance and polymorphism, on top of Elixir's structs. · GitHub
Hex: classy_structs | Hex
Let me know what you think ![]()
Cheers,
Tim
Trending in Announcing
Flop is an Elixir library that applies filtering, ordering and pagination parameters to your Ecto queries.
offset-based pagination with...
New
I needed to reuse React components from my Chrome extension in my Phoenix/LiveView backend. I noticed that for Svelte/Vue, there are live...
New
I released Doggo, a collection of unstyled Phoenix components.
https://github.com/woylie/doggo
Features
Unstyled Phoenix components....
New
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
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
Hello
Published a new library - ProcessHub!
ProcessHub is a library designed to manage process distribution within the Elixir cluster. ...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself.
My main conc...
New
It’s not that it’s vocabulary is too advanced. It’s something worse.
I get lost trying to follow even a paragraph written by Claude. It’...
New
This showed up on my feed.. anyone heard of it? Just hype?
Ox Alpha is a reasoning model designed for coding, sustained ag...
New
Hey folks,
I just published a post about Hologram’s funding and where the project goes next - the short version:
Curiosum as Main Spons...
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
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #hex
- #security










Showing Posts 1 to 8- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
hazardfn
This is a really cool example of what you can do with DSL’s in Elixir nice job!
If only we could convince you to use your powers for good!
timmolderez
Haha thanks
Not to worry, most of the Elixir code I write still is functional, but I found classes to be a good fit for one specific case.
If you’re interested in the details: I’m working on this research project that lets you do machine learning in distributed systems, and it should be easy to plug in additional machine learning algorithms. I first implemented these algorithms with structs + protocols. Eventually my code was full of defdelegates to “inherit” functions, and it got a bit messy to keep a separate struct for all fields that are common to all algorithms. Maybe there’s a better way to do it, but I’m happy with classes so far
(You can actually see the commit that refactors the structs+protocols to classes here: Refactored Marlon.LearningAlgorithm to use Classy structs (339fb1d0) · Commits · SMILE-IT / marlon-dsl · GitLab )
hazardfn
It is a lot neater (the diff).
I guess there are always use cases for these things I was just being facetious
.
Eiji
Hey @timmolderez. I do not have a time yet to look enough deeply, but this reminds me another project called oop. Can you say if it’s similar and if so what a difference you have regarding to first one?
timmolderez
Hey Eiji,
The oop project does provide a similar feature set, but they’re quite different under the hood.
My intent with Classy structs is to provide object-oriented features, while sticking as close as possible to vanilla Elixir. In particular, class instances are directly represented as structs, which means class instances are immutable and all fields are public.
I think the intent behind the oop project is more to show that you can do full-blown OOP in Elixir. If I understand correctly, the oop project represents class instances as GenServers, which is what makes it possible to have a mutable data structure and private fields.
OvermindDL1
Yeah this sounds like entirely the wrong usage of protocols. This actually sounds like the stock erlang/elixir domain of Behaviours and First-Class Modules. I.E. you make a behaviour that the MLAlgo modules need to follow, and you pass the name of the MLAlgo modules around and call functions on it instead. Protocols are for dynamic dispatch based on data, Behaviours and First-Class Modules are for dynamic dispatch based on a module.
timmolderez
I’m not sure if it would make a difference in the end. Each of these MLAlgo modules indeed implement a behaviour, but they’re also a data type. (because each MLAlgo represents what it has learnt so far in its own kind of data structure) It’s hard to keep them separated in this case
OvermindDL1
This would be a perfect case for tuple-calls, if only Elixir and OTP both were not trying to break them… >.>
But are you needing to ‘dispatch’ on the data type though is the question? You could of course carry them around in a tuple regardless since you know all calls will involve them.