No type errors reported when list literal has elements not matching declared generic type. Why?

Patrice Chalin

I have been making my way through Shailen Tuli's collection of of snippets in Dart By Example, when I came across the following code:

List<String> fruits = [
  {'name': 'apple', 'selected': true},
  {'name': 'banana', 'selected': true},
  {'name': 'kiwi', 'selected': false}
 ];

Notice that fruits is declared to be a List<String> but that the list elements are actually maps.

Question: Why is there no type error being reported?

Patrice Chalin

If the code example had been written in a strongly statically typed language then an error would be reported. But Dart is a:

  • Dynamically typed language with
  • Static type checking based an "optimistic system of type heuristics" 1.

Consider this declaration:

List<num> lst = [4, "ty-two"];

It is important to understand that the array literal [4, "ty-two"], as given, is of type List<dynamic>, where dynamic is the static type attributed to an expression when the static type system does not know what it is (e.g., the user did not declare a static type), or doesn't want to bother doing a deeper analysis to figure out what the type might be -- as is the case for the generic parameter to the list lst.

The static type system can only help point out that there is a type error if you declare the expected static type of the literal list's elements like this:

List<num> lst = <num>[4, "tytoo"];
List<String> fruits = <Map>[{'name': 'apple', ...}, ...];

Now, in both cases a type error is reported. In the case of lst the optimistic system can easily tell that the String "ty-two" can never be of type num. Similar remarks apply for fruits. On the other hand if we had

var a = "tytoo";
List<num> lst = <num>[4, a];

To declare a variable with a var effectively means (to the static type checker) that a is of type dynamic. Hence, no error is reported in the assignment to lst.

Details are given in the Language Specification and these two (IMHO) excellent articles; they explain typing in Dart while also covering the rationale behind the language design decisions that were taken relative to the type system.

--

1 https://www.dartlang.org/articles/why-dart-types/
2 https://www.dartlang.org/articles/optional-types/

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why literal value has no type?

From Dev

Difference between errors "does not name a type" and "has not been declared"

From Dev

Difference between errors "does not name a type" and "has not been declared"

From Dev

Scala generic type matching

From Dev

Scala generic type matching

From Dev

Generic method type inference when the target type has a wildcard

From Dev

Generic method type inference when the target type has a wildcard

From Dev

Action has no declared type error

From Dev

Why is the base type not returned when no generic type specified?

From Dev

Why isn't `std::initializer_list` defined as a literal type?

From Dev

Pattern matching on generic type in Scala

From Dev

Generic argument type matching in java

From Dev

Generic argument type matching in java

From Dev

Generic pattern matching and type erasure

From Dev

Scala generic type method matching

From Dev

Why is generic type not the correct type?

From Dev

Compiler errors when using method reference as lambda to methods that expect interface with generic type, when runtime type is also generic type

From Dev

Generic type in a list of functions

From Dev

Generic type in a list of functions

From Dev

Typescript pattern matching with subtype of string literal type

From Dev

Instantiate generic c# List when the reflected type itself is a List

From Dev

Casting a list of an unknown type to a generic list type

From Dev

How to get the type of the elements in a declared TList

From Dev

Why do I have to parent-type-qualify to use a nested type when inheriting from a generic type?

From Dev

Why do I have to parent-type-qualify to use a nested type when inheriting from a generic type?

From Dev

Why does Scala not infer the type parameters when pattern matching with @

From Dev

Why is it needed to cast a generic type when a where constraint should be enough

From Dev

Restrict type of elements of list

From Dev

Matching a generic parameter to an associated type in an impl

Related Related

  1. 1

    Why literal value has no type?

  2. 2

    Difference between errors "does not name a type" and "has not been declared"

  3. 3

    Difference between errors "does not name a type" and "has not been declared"

  4. 4

    Scala generic type matching

  5. 5

    Scala generic type matching

  6. 6

    Generic method type inference when the target type has a wildcard

  7. 7

    Generic method type inference when the target type has a wildcard

  8. 8

    Action has no declared type error

  9. 9

    Why is the base type not returned when no generic type specified?

  10. 10

    Why isn't `std::initializer_list` defined as a literal type?

  11. 11

    Pattern matching on generic type in Scala

  12. 12

    Generic argument type matching in java

  13. 13

    Generic argument type matching in java

  14. 14

    Generic pattern matching and type erasure

  15. 15

    Scala generic type method matching

  16. 16

    Why is generic type not the correct type?

  17. 17

    Compiler errors when using method reference as lambda to methods that expect interface with generic type, when runtime type is also generic type

  18. 18

    Generic type in a list of functions

  19. 19

    Generic type in a list of functions

  20. 20

    Typescript pattern matching with subtype of string literal type

  21. 21

    Instantiate generic c# List when the reflected type itself is a List

  22. 22

    Casting a list of an unknown type to a generic list type

  23. 23

    How to get the type of the elements in a declared TList

  24. 24

    Why do I have to parent-type-qualify to use a nested type when inheriting from a generic type?

  25. 25

    Why do I have to parent-type-qualify to use a nested type when inheriting from a generic type?

  26. 26

    Why does Scala not infer the type parameters when pattern matching with @

  27. 27

    Why is it needed to cast a generic type when a where constraint should be enough

  28. 28

    Restrict type of elements of list

  29. 29

    Matching a generic parameter to an associated type in an impl

HotTag

Archive