timmolderez
Classy structs: inheritance and polymorphism on top of structs
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
Most Liked
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
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. ![]()
Last Post!
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. ![]()
Popular in Announcing
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #hex









