News for hackers. Stuff that matters.
Re:Making a Go program 70% faster with a one character change by kapep on Monday November 14, 2022 @05:06PM (#33601926)
I had to look it up too, it apparently is a constraint for subtypes defined in Liskovs substitution principle [1]. From Wikipedia:

> History constraint (the "history rule"). Objects are regarded as being modifiable only through their methods (encapsulation). Because subtypes may introduce methods that are not present in the supertype, the introduction of these methods may allow state changes in the subtype that are not permissible in the supertype. The history constraint prohibits this. It was the novel element introduced by Liskov and Wing. A violation of this constraint can be exemplified by defining a mutable point as a subtype of an immutable point. This is a violation of the history constraint, because in the history of the immutable point, the state is always the same after creation, so it cannot include the history of a mutable point in general. Fields added to the subtype may however be safely modified because they are not observable through the supertype methods. Thus, one can define a circle with immutable center and mutable radius as a subtype of an immutable point without violating the history constraint.

[1]: https://en.wikipedia.org/wiki/Liskov_substitution_principle#...

Re:Making a Go program 70% faster with a one character change by aatd86 on Tuesday November 15, 2022 @08:54AM (#33609167)
Just curious, isn't it obvious from a logical standpoint? I don't see how one could consider a mutable type to be a subtype of an immutable one. On the other hand, an immutable subtype of a mutable one seem plausible?
Re:Making a Go program 70% faster with a one character change by cogman10 on Tuesday November 15, 2022 @02:48PM (#33614058)
Immutable from a mutable is just as implausible. You cannot remove a method from a subtype which results in mutating methods being disabled through other means (like throwing exceptions).

This is also a violation of LSP and the open close principle.

Consider a `List` with an `add` function. What would you do with that `add` function to make an `ImmutableList` subtype?

Re:Making a Go program 70% faster with a one character change by aatd86 on Wednesday November 16, 2022 @06:31PM (#33632560)
Ah I see. Why throw exceptions? They could just be noop depending on the typestate?

But still it doesn't look very nice in practice. I think that in that case, it does make much more sense for a mutable type to be a subtype of the immutable one.

The immutable threw me off track. It's just that the supertype would lack mutation operations. (in a sense immutable would be the default for every type).

Thank you.