News for hackers. Stuff that matters.
Re:Making a Go program 70% faster with a one character change by morelisp on Monday November 14, 2022 @03:07PM (#33600198)
In general this doesn't work, the history rule says mutable types are not proper subtypes of immutable ones (and the converse is obvious). If you want to capture mutability in your type system, it needs to be orthogonal to subtyping (like C/C++ const).
Re:Making a Go program 70% faster with a one character change by a_t48 on Monday November 14, 2022 @03:26PM (#33600492)
C++ still has this problem - std::unordered_map<std::string, std::string>` and `std::unordered_map<std::string, const std::string>` are basically unrelated types - you can't const-cast the templated const away. (I may be misunderstanding here)
Re:Making a Go program 70% faster with a one character change by masklinn on Monday November 14, 2022 @03:50PM (#33600815)
> you can't const-cast the templated const away.

That seems like a good thing. If you're handed a map to const values you can't just go "imma gunna mutate them anyway".

Re:Making a Go program 70% faster with a one character change by a_t48 on Monday November 14, 2022 @04:25PM (#33601360)
Yup, it's definitely a bit of a code smell if you do. The issue is more the reverse, though - I can't make a mutable map, then hand it by pointer/reference to something that says it wants an immutable map later.
Re:Making a Go program 70% faster with a one character change by masklinn on Monday November 14, 2022 @04:28PM (#33601393)
Not necessarily a bad thing either, things can get odd if you're not the only owner and it's mutated under you unexpectedly.
Re:Making a Go program 70% faster with a one character change by a_t48 on Monday November 14, 2022 @04:38PM (#33601543)
That only matters if you're storing it. It mainly just encourages never using a map with constant values. As an API writer it would be neat to be able to accept a `const unordered_map<key, const val>&` to promise "I won't touch the map nor its contents" but doing so mans you aren't compatible with most real world usages of map. It matters also if you want to make a copy of the map. Imagine I want a function `void maybeMakeCopyAndStore(const unordered_map<std::string, const ExpensiveStruct>&`. As I don't want to always make a copy, I don't want to accept a moved value. I would like to promise to my caller that I don't touch the passed in struct. But I also need to be compatible with caller code that likely mutates the values.
Re:Making a Go program 70% faster with a one character change by a_t48 on Monday November 14, 2022 @04:50PM (#33601726)
That only matters if you're storing it. The big issue is 99% of code out there uses mutable template types for containers, and if you ever declare a container that doesn't have a mutable template type, you stub your toe as your new container isn't compatible with anyone else's code. You can't even easily copy your way out.

  std::unordered_map<std::string, const std::string> m;
  m["foo"] = "bar";
  std::unordered_map<std::string, std::string> m2 = m;
doesn't compile.
Re:Making a Go program 70% faster with a one character change by account42 on Tuesday November 15, 2022 @04:03AM (#33606907)
> That only matters if you're storing it.

Or if you (or something you call) mutate it trough another reference/pointer. Sometimes I wish there was a stronger const where the compiler (and programmer) could actually assume the object is not mutated while that reference/pointer is alive. Of course checking that is no longer doable with just the type system.

> You can't even easily copy your way out.

That depends on the type. std::unordered_maps could provide a copy constructor that does the conversion.

And you can do the copy via iterators, although that is probably not as efficient as it could be because it needs to re-hash the keys.

  std::unordered_map<std::string, std::string> m2(m.begin(), m.end());
Re:Making a Go program 70% faster with a one character change by a_t48 on Tuesday November 15, 2022 @02:50PM (#33614081)
Sure certainly, copying via iterators work, it's just sad and very not implicit.
Re:Making a Go program 70% faster with a one character change by morelisp on Monday November 14, 2022 @04:04PM (#33601023)
Normally (when containers are not involved) this is exactly the point of a const cast.
Re:Making a Go program 70% faster with a one character change by account42 on Tuesday November 15, 2022 @04:11AM (#33606957)
No, the primary point of a const cast is "I need to pass this to an API that expects a non-const pointer even though it won't mutate it".

Casting const away and then mutating is a footgun as you are in undefined behavior territory as soon as your pointer/reference is not just const itself but points to a const variable.

Re:Making a Go program 70% faster with a one character change by cogman10 on Monday November 14, 2022 @03:58PM (#33600927)
> the history rule

I'm unfamiliar with this rule (and not finding anything good to google). Can you elaborate?

I can't really think of a scenario where an immutable datastructure isn't a subset of actions against a mutable datastructure.

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.