timmolderez

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 :slight_smile:

Cheers,
Tim

Showing Posts 1 to 8

hazardfn

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! :wink:

timmolderez

timmolderez OP

Haha thanks :stuck_out_tongue:

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 :slight_smile: (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

hazardfn

It is a lot neater (the diff).

I guess there are always use cases for these things I was just being facetious :stuck_out_tongue:.

Eiji

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

timmolderez OP

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

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. :slight_smile:

timmolderez

timmolderez OP

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 :slight_smile:

OvermindDL1

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. :slight_smile:

— All posts loaded —

Where Next? Top

Trending in Announcing Top

woylie
Flop is an Elixir library that applies filtering, ordering and pagination parameters to your Ecto queries. offset-based pagination with...
New
MRdotB
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
woylie
I released Doggo, a collection of unstyled Phoenix components. https://github.com/woylie/doggo Features Unstyled Phoenix components....
New
JesseHerrick
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
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
anuaralfetahe
Hello Published a new library - ProcessHub! ProcessHub is a library designed to manage process distribution within the Elixir cluster. ...
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

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
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New
sergio
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
AstonJ
This showed up on my feed.. anyone heard of it? Just hype? Ox Alpha is a reasoning model designed for coding, sustained ag...
New
bartblast
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

Latest on Elixir Forum

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews