Closed gintominto5329 closed 1 year ago
Sounds very much related to: https://github.com/dart-lang/language/issues/1014
"Non-growable lists are implemented more efficiently than growable lists, but creating one is rather verbose and inconvenient"
Fwiw this isn't universally true - on the web at least non-growable lists may actually add additional checks since it uses a growable JS array, which it then has to ensure doesn't actually grow.
I think the desire for this feature is valid regardless though.
Closing as dupe of #1014.
(In practice, the typical workaround is to just define a function that takes a List. Since Dart has very compact list literals, that's only two characters longer than varargs.)
Hello,
In other, and mature languages like C/CPP, and Java, there is a concept of variable arguments(va),
Prominent use in existing(and mature) langs:
va_list
is used in thestdio.h
'sprintf(format, ...)
function, which takes a minimum of one, and a super-large(but finite) number of additional arguments, andvarargs
are used injava.util.Arrays.asList(T... a)
Why?
Dart is facing a problem, which is "Non-growable lists are implemented more efficiently than growable lists, but creating one is rather verbose and inconvenient"(from #2477(#2477)),
The new syntax, being proposed in that issue, is:
<int>[0, 1, 2]
for growable list, same as current/presentfinal <int>[0, 1, 2]
for non-growable/fixed-length list/arrayconst <int>[0, 1, 2]
for non-modifiable/immutable listProblem
The reserved words,
final
, andconst
are for, and act on the underlyingObject
, which isList<int>
here, and has no relation with the grow-ability or im-mutability of the entity/list, the proposed syntax seem like patchy work to me, and would definitely degrade the quality/logic in the syntax, and the trust of public, in dart's teamSolution
Adding var-args to dart would allow us to:
List<int>(0, 1, 2)
for growable list, with def asList<T>(T... va)
Array<int>(0, 1, 2)
for non-growable/fixed-length list/arrayFirmList<int>(0, 1, 2)
for non-modifiable/immutable listArrayList
overArray
, is possible, but more chars, and same forImmutableList
/UnmodifiableList
overFirmList
, maybeImmuList
/UnmodList
could be used,thanks
va_list
in C: cppreference.com, cprogramming.comVarargs
in Java: docs.oracle.com