4 ms·
A 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);
by dchest 15y ago
A 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.