dart-lang / language

Design of the Dart language
Other
2.65k stars 201 forks source link

[NEW] variable argument list, like `va_list` in C, and `Varargs` in Java #2821

Closed gintominto5329 closed 1 year ago

gintominto5329 commented 1 year ago

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:

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:

Problem

The reserved words, final, and const are for, and act on the underlying Object, which is List<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 team

Solution

Adding var-args to dart would allow us to:

ArrayList over Array, is possible, but more chars, and same for ImmutableList/UnmodifiableList over FirmList, maybe ImmuList/UnmodList could be used,

thanks

va_list in C: cppreference.com, cprogramming.com

Varargs in Java: docs.oracle.com

julemand101 commented 1 year ago

Sounds very much related to: https://github.com/dart-lang/language/issues/1014

jakemac53 commented 1 year ago

"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.

munificent commented 1 year ago

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.)