4 ms·
> > * Mutable strings > I love these! No need for messing around with `StringBuilder` and the like. D has immutable strings and no StringBuilder. Same with Py
by atilaneves 8y ago
> > * Mutable strings
> I love these! No need for messing around with `StringBuilder` and the like.
D has immutable strings and no StringBuilder. Same with Python. Concatenating isn't exactly hard.
- dom96 8y agoIn my experience code that needs to generate strings in Python would usually use a list of strings and then use `join` at the end. What's the problem with just making strings mutable?
- atilaneves 8y agoThe usual problems with making things mutable: that mutable state is the root of all evil.
- ernst_klim 8y ago>What's the problem with just making strings mutable? They are not arrays of characters until you use ASCII/KOI8 or similar encodings. So there is no point in mutating them, you need unicode normalization facilities, formatters and stuff to deal with them properly. And a proper mutable unicode string would give you a terrible performance penalty.
- kbd 8y agoIt 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.
- baq 8y agocan't use a mutable string as a map key and remain sane at the same time.
- adev_ 8y agoThe last language I would take as an example for string is python. The disastrous handling of the transition python2 to python3 with UTF-8 string and the idiosyncratic nonsense like " ''.join([str1, str2]) to concat anything speaks for themselves.
- nudpiedo 8y agoIn python you can concatenarme strings using the operator plus, using templates, the method join, putting them in buffers or streams and many other ways just using the main python builtin functions just to name a few (no std library yet!)... the support for slicing is also very good and compare strings has a cost O(1) due comparing IDs instead of their content every time (optimize thanks to immutably as property). Python 2 had several shortcomings, but not with strings I think. The transition to python 3 is more more a matter about a migration execution rather than a technical aspect related to strings (which I don’t find that terrible, especially given all facilities provided to make the migration easier to the new incompatible runtime).
- adev_ 8y ago> The transition to python 3 is more more a matter about a migration execution rather than a technical aspect related to strings (which I don’t find that terrible, especially given all facilities provided to make the migration easier to the new incompatible runtime). That's political talk. In practice I never have seen a python library that do not blow up when migrated in Python 3 with a Unicode related error, because one of the dev was using python2 string as binary vector.
- nudpiedo 8y agoyou are underestimating python; it has many ways of concatenating strings, such as operators, buffers, templates, streams, the join method etc also. By having mutable strings you lose the chance of relying on them as hashable objects and constant cost for comparison and several memory optimizations for duplicates and other optimizations when you have a runtime. There is a lot to win in the big game (or perhaps not that much if someone is thinking too much into a constrained system). mutable string: a buffer made of chars which can be modified on demand with little memory immutable string: some abstract data which ensures some properties that can be leveraged for many purposes