Skip to content
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

Growable list constructor #21406

Open
DartBot opened this issue Oct 24, 2014 · 6 comments
Open

Growable list constructor #21406

DartBot opened this issue Oct 24, 2014 · 6 comments
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. core-n library-core type-enhancement A request for a change that isn't a bug

Comments

@DartBot
Copy link

DartBot commented Oct 24, 2014

This issue was originally filed by sugashi.s...@gmail.com


What steps will clearly show the issue / need for enhancement?

  1. add 'growable' parameter to new List()
    2.
    3.

What is the current output?

  var list = new List()..length = 10;

What would you like to see instead?

  var list = new List(10, growable:true);

Please provide any additional information below.

  growable should be true in default.

@lrhn
Copy link
Member

lrhn commented Oct 24, 2014

The current List constructor is designed the way it is in order to allow compilers to statically detect whether the resulting list is growable or not.

That is why new List(null) is not the same as new List(), even if the constructor appears to be declared as factory List([int n]).
If we add another parameter, it would not be possible to detect the type of list created in all cases (if the growable parameter isn't a compile-time constant).

That said, I'm not unsympathetic to the suggestion, even if it will add overhead to the simple case.

The growable parameter has to default to null, so that the constructor retains its current behavior - growable if no arguments and not growable if one argument. With two arguments, it will then depend on the value of growable.


Added Library-Core, Triaged labels.

@lrhn
Copy link
Member

lrhn commented Oct 24, 2014

One complication, which is probably fatal for this idea, is that we can't add growable as a named parameter, because the function already has an optional positional parameter, and Dart doesn't allow both optional positional and optional named parameters on the same function.

@DartBot
Copy link
Author

DartBot commented Oct 24, 2014

This comment was originally written by sugashi.swi...@gmail.com


Thanks.I understand why cannot add growable as a named parameter.

But I want to know a possibility if growable are the second positional paramerter.

that means,

new List(); //growable and empty
new List(10); //fixed
new List(10, true); //growable

@lrhn
Copy link
Member

lrhn commented Oct 28, 2014

It's possible, but I think it would not be desirable.
All other places where a "growable" parameter is used, it is named. Having it positional here, just because it can't be named, will be confusing and error-prone.

Since an easy workaround is to create a factory method:

    List makeList(int length, {bool growable: true}) =>
        growable ? []..length = length : new List(length);

and use that instead of new List, it's not an urgent need.

(It's slightly harder if you want type parameters:
    class ListMaker<T> { create(int length, {bool growable: true}) =>
                          growable ? <T>[]..length = length : new List<T>(length); }
then replace new List<T>(..) with new ListMaker<T>.create(length, growable: false).

I'd rather wait until Dart allows both optional named and positional arguments on the same function (if that ever happens, but I'll keep crossing my fingers, and starring http://dartbug.com/7056).


Added label.

@lrhn
Copy link
Member

lrhn commented Oct 28, 2014

Added Triaged label.
Marked this as being blocked by #7056.

@floitschG
Copy link
Contributor

Fwiw it should be easy to just write: []..length=XY;
The VM and dart2js could recognize that pattern.

@kevmoo kevmoo added the area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. label Jun 16, 2015
@kevmoo kevmoo added type-enhancement A request for a change that isn't a bug and removed triaged labels Mar 1, 2016
@floitschG floitschG added core-n and removed core-m labels Aug 24, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. core-n library-core type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

4 participants