Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
gintominto5329 opened this issue Feb 6, 2023 · 3 comments
Closed
Labels
feature Proposed language feature that solves one or more problems

Comments

@gintominto5329
Copy link

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:

  • In C, va_list is used in the stdio.h's printf(format, ...) function, which takes a minimum of one, and a super-large(but finite) number of additional arguments, and
  • In Java, varargs are used in java.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/present
  • [NEW] final <int>[0, 1, 2] for non-growable/fixed-length list/array
  • [NEW] const <int>[0, 1, 2] for non-modifiable/immutable list

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:

  • List<int>(0, 1, 2) for growable list, with def as List<T>(T... va)
  • Array<int>(0, 1, 2) for non-growable/fixed-length list/array
  • FirmList<int>(0, 1, 2) for non-modifiable/immutable list

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

@gintominto5329 gintominto5329 added the feature Proposed language feature that solves one or more problems label Feb 6, 2023
@julemand101
Copy link

Sounds very much related to: #1014

@jakemac53
Copy link
Contributor

"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
Copy link
Member

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

@munificent munificent closed this as not planned Won't fix, can't repro, duplicate, stale Feb 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Proposed language feature that solves one or more problems
Projects
None yet
Development

No branches or pull requests

4 participants