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: https://gitlab.soft.vub.ac.be/smileit/marlon-dsl/commit/339fb1d02c26b7ac88abcd4eb2e0a684e31a6bd3#9af9cee2b42b228e6488ab965fe8db2044c824a0 )
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?
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.
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.
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
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.