Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subclasses in clojure

I'm learning Clojure and I was wondering how to deal with OO-like subclasses in Clojure. For example: a master abstract class, two abstract subclasses (each one redefines some functions) and in the 3rd level, final subclasses that creates "objects" that will be used in the functions. No clue how to do this. However, I managed to do it with one abstract class to a child class, with defprotocol and defrecord. But I can't implement a protocol inside another. Thanks

like image 587
juanimoli Avatar asked May 14 '26 05:05

juanimoli


1 Answers

You don't need classes or subclasses. Represent your data as maps with attributes. The "subclasses" might have more attributes.

If you have a function that varies on attribute, then either use conditional logic based on attribute (if, cond, etc) or use polymorphism based on multimethods or protocols if you really need to.

In the words of the Matrix, there is no spoon.

like image 96
Alex Miller Avatar answered May 15 '26 18:05

Alex Miller



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!