Skip to content

[NNBD] default List constructor #509

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
sgrekhov opened this issue Aug 9, 2019 · 1 comment
Closed

[NNBD] default List constructor #509

sgrekhov opened this issue Aug 9, 2019 · 1 comment

Comments

@sgrekhov
Copy link
Contributor

sgrekhov commented Aug 9, 2019

According to the NNBD spec

It is an error to call the default List constructor with a length argument and a type argument which is potentially non-nullable.

There are two conditions conbined with AND. It looks strange for me. Please consider the following code.

class A {}

class C<X extends A?> {
  X x;
  C(this.x);

  test() {
    List l1 = new List<X>();     // type X is potentially non-nullable but no length argument specified. No error?
    List l2 = new List<X>(0);    // type X is potentially non-nullable, argument specified. Is this an error then?
    List l3 = <X>[];    // what about this?
    var l4 = <X>[x];    // and this?
}

main() {
  A? a = new A();
  C<A?> c = new C<A?>(a);
  c.test();
}
@eernstg
Copy link
Member

eernstg commented Aug 14, 2019

Cf. discussions at the meeting yesterday: The problem is that the length argument implies that the new list should have that many elements, but a potentially non-nullable type argument does not allow for any values to be used (can't use null, and no other values can be used as a natural default value for an unknown type like X extends Object).

Also, List<X>(0) is an error even though it is safe, because the error check does not bother to discover that this particular argument value is safe; List<X>(1) must be an error because we can't obtain a value of type X out of the blue.

List<X>() is OK following the spec text on the error.

Finally, list literals are irrelevant, because this error is only concerned with explicit invocations of that particular List constructor.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants