Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to test whether a Dart List is growable?

Tags:

dart

Given a List, is it possible to test whether the list is growable?

Trying to set the length and catching an UnsupportedError seems like a solution (though it isn't clear what would happen if you just set the length to the same value). Any better solution?

like image 941
Patrice Chalin Avatar asked Sep 01 '25 10:09

Patrice Chalin


1 Answers

There is no way to detect if a list is growable (short of using reflection to find the implementation type, which is brittle, won't work the same way in dart2js, and increases code size).

The only valid use-case we encountered was to have checks/asserts when a library returns a list. In all other cases a function/library tried to modify an argument without knowing if it was allowed to do that.

If a function/library can work destructively it should require a boolean (or similar) so that the callers can decide if their argument can be changed. The callee should never silently modify its inputs unless it is obvious (for example fillFoo(list)) or an argument tells it so (for instance computeSquares(list, inPlace: true)).

http://dartbug.com/13926 is still open, but I expect it to be closed tomorrow with status "NotPlanned".

like image 119
Florian Loitsch Avatar answered Sep 03 '25 18:09

Florian Loitsch