Skip to content

Subtracting lists of different types #191

@angel333

Description

@angel333

Shouldn't all of these work?

class A {}

class B implements A {}

// Works fine...
listOf<A>(A()).minus(listOf<A>(B()));

// _TypeError (type 'A' is not a subtype of type 'B' of 'element')
listOf<A>(A()).minus(listOf<B>(B()));

// Not sure why but this one will throw as well.
listOf<A>(A()).minus(listOf<B>(B()).cast<A>());

// This one works, though.
// ignore: unnecessary_cast
listOf<A>(A()).minus(listOf<B>(B()).map((element) => element as A));

Relevant stack trace:

DartList.contains (...\kt_dart-1.0.0\lib\src\collection\impl\list.dart:0)
KtIterableExtensions.contains (...\kt_dart-1.0.0\lib\src\collection\kt_iterable.dart:336)
KtIterableExtensions.minus.<anonymous closure> (...\kt_dart-1.0.0\lib\src\collection\kt_iterable.dart:1031)
KtIterableExtensions.filterNotTo (...\kt_dart-1.0.0\lib\src\collection\kt_iterable.dart:536)
KtIterableExtensions.filterNot (...\kt_dart-1.0.0\lib\src\collection\kt_iterable.dart:511)
KtIterableExtensions.minus (...\kt_dart-1.0.0\lib\src\collection\kt_iterable.dart:1031)

The reason why I'm asking is actually Dart's type inference. Depending on how the type is inferred, seemingly similar code might or might not work.

Type inferred from the minus() signature - works fine:

listOf(A()).minus(listOf(B()));

Type inferred from the listOf() signature - won't work:

final listOfA = listOf(A());
final listOfB = listOf(B());
listOfA.minus(listOfB);

I tried to find what exactly is going on but honestly, I'm lost :). I can write a test if needed, though. I'm on Dart 2.18.2.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions