Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Subtracting lists of different types #191

Open
angel333 opened this issue Dec 14, 2022 · 0 comments
Open

Subtracting lists of different types #191

angel333 opened this issue Dec 14, 2022 · 0 comments

Comments

@angel333
Copy link

angel333 commented Dec 14, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant