Skip to content

Would like a way to avoid raw generic types #33119

@matanlurey

Description

@matanlurey

Tangentially related to #31410.

While migrating swaths of internal code (probably in the tens-of-thousands-of-lines, myself), one of the most common errors users are running into when migrating to Dart2 are so-called "naked" raw types (is there a better name?). For example:

// Works as the user intended.
// List<int> x = <int>[1];
var x = [1];
// Does _not_ work as the user intended.
// List<dynamic> x = <dynamic>[1];
List x = [1];

Here is a more extreme example:

typedef Iterable<CacheEntry<K, V>> CacheEvictionFunction<K, V>(
  List<CacheEntry<K, V>> entries, 
  int entriesToBeTrimmed,
);

class CacheConfig {
  // In Dart1 this was "fine".
  // In Dart2 this fails, quite late, at runtime:
  // 
  // CacheEvictionFunction<dynamic, dynamic> "inferred"
  final CacheEvictionFunction _evictionImpl;

  CacheConfig(this._evictionImpl);
}

My 🔥 take: In Dart 2 + some flag, "raw" types that are implicitly dynamic should:

  • Be a compile-time error ("You must specify a type or ")
  • Work similar to Java's <?> (wildcard operator). That would mean:
// --no-implicit-dynamic
//
// List<int> x = <int>[1];
List x = [1];
// --no-implicit-dynamic
//
// Compile-time error: Must specify a type 'T' for List<T>
List x;
// --no-implicit-dynamic
//
// OK.
List<dynamic> x;

... if we are accepting name nominations, I nominate --strict-raw-types.

Metadata

Metadata

Labels

P1A high priority bug; for example, a single project is unusable or has many test failuresarea-languageDart language related items (some items might be better tracked at github.com/dart-lang/language).customer-google3devexp-linterIssues with the analyzer's support for the linter packagestrict-analysis-neededMark issues where strict-raw-types, strict-inference and similar analysis options were helpful

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions