3 ms·
Why are they using a type (String) for a constant (''final'' variable)? It's not a constant. Is a variable that contains a value of string type that cannot be
by dchest 15y ago
Why are they using a type (String) for a constant (''final'' variable)?
It's not a constant. Is a variable that contains a value of string type that cannot be assigned to anymore. "String" is an annotation for variable, not for the value.
There is a separate string builder class ''StringBuffer'' (which does not share the interface with ''String'', I don't know why not,
Because string values are immutable, they can't be concatenated without lots of allocations. StringBuffer solves this.
This document, to me, screams of technical incompetency, or at least of language design from 10 years ago.
This is probably because you didn't think out the various trade-offs and design decisions, of which Dart creators thought.
- tomp 15y ago> This is probably because you didn't think out the various trade-offs and design decisions I did, I just didn't want to write too many reasons, just a short rant... > It's not a constant. A variable that can only be assigned when it is declared, is not a constant? I think there's a fundamental disagreement in our life philosophies that can not be resolved through debate... ;) > string values are immutable, they can't be concatenated without lots of allocations A much more elegant approach that solves most string concatenation problems is string interpolation, which Dart has. There are legitimate uses of StringBuffer, but I'm not sure why they need to mention it in a Language Tutorial. However, my real complaint is not that. They took the time to put int and double under a common interface, num. So, why not put concrete string and StringBuffer under a common interface, called string, and also have StringBuffer implement a monoid interface (for the + and * operations). Edit: I hope this doesn't come off as rude. Just tried to clarify my original points.
- dchest 15y agoA variable that can only be assigned when it is declared, is not a constant? main() { final v = [1, 2, 3]; print(v.length); // => 3 v.add(4); print(v.length); // => 4 } There are constant expressions (and values) in Dart, though. For example, you cannot assign a mutable list (array) to 'final v' at the top-level, because it's not constant. However, my real complaint is not that. They took the time to put int and double under a common interface, num. So, why not put concrete string and StringBuffer under a common interface, called string, and also have StringBuffer implement a monoid interface (for the + and operations).* Then what happens when you want it as a key in the map? Really, string buffers and strings are different concepts. Even Ruby, with its mutable strings, has StringIO. Edit: I hope this doesn't come off as rude. Just tried to clarify my original points. Of course not, we're having a great discussion.
- tomp 15y agoGood points. I guess it really depends what one considers immutable and mutable... Is an immutable list/tuple that contains mutable structures, a mutable or immutable object? The second issue, happens everytime you try to put a mutable object into a hashtable. I get bit by this issue quite often in python. In my opinion, there should be a default toMutable method that would make mutable objects immutable, and the map would then store the immutable version. That way, you could store e.g. Python lists in dicts, and have them compared by equality.