Skip to content

Strong mode generics - how to apply correctly #25100

Closed
@zoechi

Description

@zoechi

The code below produces a few errors I don't know how to fix.

UnmodifiableListView<int> _ioServiceBytesRecursive;

_ioServiceBytesRecursive = toUnmodifiableListView /*<int>*/ (json['io_service_bytes_recursive'] as Iterable);
//  WARNING: toUnmodifiableListView<int>(json['io_service_bytes_recursive'] as Iterable) (UnmodifiableListView<dynamic>) will need runtime check to cast to type UnmodifiableListView<int> ([bwu_docker] lib/src/shared/data_structures.dart:138)
...

UnmodifiableListView /*<T>*/ toUnmodifiableListView /*<T>*/ (Iterable list) {
  if (list == null) {
    return null;
  }
  if (list.length == 0) {
    return new UnmodifiableListView<dynamic /*=T*/ >(<dynamic /*=T*/ >[]);
  }

  return new UnmodifiableListView<dynamic /*=T*/ >(
      list.map /*<T>*/ ((dynamic e) {
    if (e is Map) {
      return toUnmodifiableMapView(e) as dynamic /*=T*/; 
// ERROR: Type check failed: toUnmodifiableMapView(e) as T (T) is not of type T ([bwu_docker] lib/src/shared/json_util.dart:143)
// WARNING: The return type 'T (/home/myuser/dart/bwu_docker/lib/src/shared/json_util.dart)' is not a 'T (/usr/local/apps/dart/dart-sdk/lib/core/iterable.dart)', as defined by the method '' ([bwu_docker] lib/src/shared/json_util.dart:143)

    } else if (e is List) {
      return toUnmodifiableListView (e) as dynamic /*=T*/;
// WARNING: The return type 'T (/home/myuser/dart/bwu_docker/lib/src/shared/json_util.dart)' is not a 'T (/usr/local/apps/dart/dart-sdk/lib/core/iterable.dart)', as defined by the method '' ([bwu_docker] lib/src/shared/json_util.dart:145)
// ERROR: Type check failed: toUnmodifiableListView(e) as T (T) is not of type T ([bwu_docker] lib/src/shared/json_util.dart:145)

    } else {
      return e as dynamic /*=T*/;
// WARNING: The return type 'T (/home/myuser/dart/bwu_docker/lib/src/shared/json_util.dart)' is not a 'T (/usr/local/apps/dart/dart-sdk/lib/core/iterable.dart)', as defined by the method '' ([bwu_docker] lib/src/shared/json_util.dart:147)
// ERROR: Type check failed: e as T (T) is not of type T ([bwu_docker] lib/src/shared/json_util.dart:147)
    }
  }));
}

Am I doing something wrong or is this not yet supported?

Related to

Metadata

Metadata

Assignees

Labels

P1A high priority bug; for example, a single project is unusable or has many test failureslegacy-area-analyzerUse area-devexp instead.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions