28 points by ibobev 5 days ago | 56 comments | View on ycombinator
js8 4 days ago |
bruce343434 4 days ago |
- A: can I change the value
- B: can something else change the value (can I depend on a predictable stable value)
Because the axes are orthogonal, hierarchical based subtyping (inheritance) breaks, but type classes (interfaces), ad hoc polymorphism, would work.
In C, const answers A
In rust, due to pointer aliasing restrictions (either one mut pointer xor any amount of read only pointers), (lack of) mut answers both A and B
BlackFly 4 days ago |
So mutability xor aliasing provides this strict subtyping relation. Of course, you also then need ways of loosening this by providing objects without such a contract and you enter the land of interior mutability, where again the mutable methods can be understood as a part of a subtype because a holder of the reference without mutable methods was explicitly told that there was no the guarantee that the object wouldn't change.
raincole 4 days ago |
In C# ReadOnlyCollection<T> and ImmutableArray<T> are two completely different things for this exact reason.
cubefox 4 days ago |
In contrast, neither are the mutable things a subset of the immutable things nor the other way round. It's not the case that everything mutable is immutable nor that everything immutable is mutable. The two types are disjoint.
Sharlin 4 days ago |
Mikhail_Edoshin 4 days ago |
anankaie 4 days ago |
Moreover, I suspect it is possible to construct an interface such that to prove statically that you can Liskov Substitute a type into it would be equivalent to deciding Halt: All you need are extensional semantics in your type system.
gus_massa 4 days ago |
comrade1234 4 days ago |
raffael_de 4 days ago |
MiroslavPokorny 4 days ago |
zkmon 4 days ago |
So it's kind of a categorical error. (I want to joke here that all categorical errors are just type errors in category theory.) When we speak of "type of a variable", we mean this variable can only be assigned (bound to) values of certain type. This has nothing to do with whether it can be reassigned (i.e. mutability).
So you don't even need the notion of subtyping to explain this.
Also, one could probably define variable as a monad over its type.