Ya know the more I reflect on the languages I know the more I realize that outside of functional languages none of them really handle immutability well.

Consider that you want most of your objects to be immutable most of the time. Thats all well and good till you realize you want to be able to edit the objects in such a way that it creates duplicates that have some data changed but are likewise immutable.

This tends to stop working, almost entierly, once you get into subclassing. If you parent class has a method that returns a copy of itself with some data modified, this will break in children classes, since you want children classes to return instances of itself, not its parent.

Its not that you cant fix that, but the code gets very ugly very quickly. Generally you are forced to let the code handling the classes do the copying and editing itself, but that is pretty ugly too.

I have had this pattern problem in almost every OO language i messed with, Java, Ruby, Python, etc.



Rust traits (~interfaces) bypass this problem by having the ability to refer to the type that implements this interface in prototypes of required functions (so you can say that an interface requires e.g. a function `append_element` that returns the same type as the the type that implements this interface).

Naturally, that can only work in languages where you cannot inherit from anything other than interfaces. (It still works for interfaces inheriting from other interfaces, though.)

@robryk I'd have to see it in action to really get an opinion of that.

A simple example I could easily find: doc.rust-lang.org/std/clone/tr

(That might not satisfy the "in action" part, but I guess is a reasonable pointer and I couldn't quickly come up with a better example; people don't write that much Rust in functional style.)

Sign in to participate in the conversation
Qoto Mastodon

QOTO: Question Others to Teach Ourselves
An inclusive, Academic Freedom, instance
All cultures welcome.
Hate speech and harassment strictly forbidden.