by kyllo · 11y ago · 0 comments
Collections I understand, but what's the point of immutable objects? If you're eschewing mutability, why have objects at all? The whole point of objects is to encapsulate mutable state, together with methods for mutating that state.
If you said Once created, data structures must be immutable I would be more inclined to agree.
Citation needed. I use my objects to hold immutable state with methods that does not mutate that state.
But yeah, otherwise there's no difference, really.
push : ('a queue, 'a) -> 'a queue
pop : 'a queue -> ('a queue, 'a) option
In other words, instead of mandating that state updates in objects occurs via global ambient state simply cause objects to return a new version of themselves with the suitable updated state. All your standard immutability tricks now apply. Objects maintain their meaning through their opaqueness/type-abstraction.That's what Clojure does (and what lots of other functional languages do), only they're not "objects," they're just data structures.
Again, if you're not using mutability or inheritance, then why do you need objects at all? What do objects buy you, compared to data types + functions?
In the absence of those features, isn't an object class really just a module with a data type definition and some functions in it?
In particular, the tip of this iceberg is the difficulty of comparing functions for equality.
Does any of this matter? Maybe not. Structs with recursion can get you 90% of what you want (just ask OCaml). One thing you might miss is the ability to do late binding and open recursion—up to you whether or not you care.
In the absence of objects, that's what `letrec` is for. Scheme, OCaml, and SML all have it. In Haskell `let` is `letrec` by default. Clojure has `letfn`. C has forward declarations. Without these features you wouldn't be able to write mutually recursive functions.
Here's a counterexample of runtime polymorphism without objects: http://clojure.org/runtime_polymorphism