3 ms·
It also applies to things such as using strings as dictionary keys. Immutable strings allows you to use them as keys without worrying about them being modified
by kbd 8y ago
It also applies to things such as using strings as dictionary keys. Immutable strings allows you to use them as keys without worrying about them being modified and breaking things. It's why Ruby encourages symbols as hash keys. I don't know Nim yet so I don't know what their solution is.
It's also what allows Python to do interning of strings, since they can't change.
- dom96 8y agoNim gives you the choice of whether something is immutable or not. A `Table[T, Y]` (Nim's dictionary type) simply doesn't allow the key to be modified, no matter what the key type is.
- bunderbunder 8y agoI think this is really what it comes down to in Nim. I personally prefer immutable strings, but I don't think it would be wise to force them to always be immutable in Nim, because then you'd have this weird language wart where let foo = someValue var bar = someOtherValue yields an immutable foo and a mutable bar for all types except strings. I'm guessing that's a detail that's lost on folks who haven't spent much time with Nim, because the way mutability and immutability work in Nim is different from how it works in any mainstream language.
- atilaneves 8y agoI don't know a lot about Nim, but I do know D, and there `string` is just an alias to `immutable(char)[]`. String literals are of this type. But one can manipulate mutable arrays of char all day long if needed.
- shalabhc 8y agoHow does Nim prevent modifying an object after it has been added to a Table? (Or is it always copied?)
- dom96 8y agoA string will be copied if stored in a Table.