Skip to content

Deferred loading & types #4581

@mkustermann

Description

@mkustermann

There's some weird inconsistencies in the way types can be used in a library if those types come from a deferred import:

import 'def.dart' deferred as D;

dynamic dynamicValue;
main() async {
  await D.loadLibrary();

  // -----------------
  // Question 1:
  // -----------------

  print(D.Foo); // No compile-time-error: Can use as type literal
  print(<D.Foo>[]); // compile-time-error: Cannot use in type expressions

  // => Why is one valid the other invalid.

  // -----------------
  // Question 2:
  // -----------------

  // I can create variables with `D.Foo` type this way:
  var variableWithFooType = D.makeFoo(); // No compile-time-error, implicit `D.Foo` type
  variableWithFooType = dynamicValue; // No compile-time-error, implicit `as D.Foo` cast.

  // But I cannot do either of these:
  D.Foo variableWithFooType = D.makeFoo(); // compile-time-error: explicit  `D.Foo` type
  variableWithFooType = dynamicValue as D.Foo; // compile-time-error: explicit `as D.Foo` cast
}

with def.dart

class Foo {}
Foo makeFoo() => Foo();

Metadata

Metadata

Assignees

No one assigned

    Labels

    requestRequests to resolve a particular developer problem

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions