Skip to content

Unhandled exception thrown if generic type is specified #2568

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

Closed
DartBot opened this issue Apr 15, 2012 · 2 comments
Closed

Unhandled exception thrown if generic type is specified #2568

DartBot opened this issue Apr 15, 2012 · 2 comments
Labels
closed-invalid Closed as we don't believe the reported issue is generally actionable

Comments

@DartBot
Copy link

DartBot commented Apr 15, 2012

This issue was originally filed by [email protected]


What steps will reproduce the problem?
If the following test program is executed then unhandled exception
is thrown. No exception is thrown if line (2) is put in comment,
and line (1) is uncommmented.

class _Iterable<E> implements Iterable<E> {
  final Iterator<E> _iterator;
  _Iterable(this._iterator);

  Iterator<E> iterator() {
    return _iterator;
  }
}

Iterable<Comparable> copy(Iterable<Comparable> iterable) {
  //return new _Iterable(iterable.iterator()); // (1) : OK

  // Unhandled exception: type '_Iterable@28152909<Comparable>' is not
  // a subtype of type 'Iterable<int>' of 'output'.
  return new _Iterable<Comparable>(iterable.iterator()); // (2) : FAIL
}

main() {
  Iterable<int> output = copy([0, 1]);
  Expect.isTrue(output.iterator().hasNext());
}

You may try the test program here http://try.dartlang.org/s/skw5

What is the expected output? What do you see instead?
Expected: no exception should be thrown, specifying generic type
should not influence on the execution of the program
Actual, Dartboard: Failed type check: type tator89da13$_Iterable is not assignable to type Iterable
Actual, Dart Editor: Unhandled exception:
type '_Iterable@28152909<Comparable>' is not a subtype of type 'Iterable<int>' of 'output'.
 0. Function: '::main' url: 'file:///C:/Dart/Projects/BugReport001/Test01/TestBugs.dart' line:18 col:30

What version of the product are you using? On what operating system?
Version 0.1.0.201204131908, Build 6557
Dart SDK version 6478
Windows 7 Home Premium, 64 bit

Please provide any additional information below.

@DartBot
Copy link
Author

DartBot commented Apr 15, 2012

This comment was originally written by [email protected]


I still think that you should reach [email protected] instead of filing these issues. In this case, your problem isn't in the copy function

Iterable<Comparable> copy(Iterable<Comparable> iterable) {
return new _Iterable<Comparable>(iterable.iterator());
}

but in the main function instead:

Iterable<int> output = copy([0, 1]);

And only in checked mode -- if you disable type checks, your program runs just fine. Specifying types MUST influence program execution in checked mode, that's its very purpose!

Let's take a look at what happens:

  1. You call 'copy' with a [0, 1], which is List, which is actually List<Dynamic> (if you'd like it to be List<int>, then you should write <int>[0, 1]). The 'copy' function expects Iterable<Comparable>, so it is OK (having Dynamic as type parameter is always OK in Dart, and passing List<int> would be just fine, because generics in Dart are covariant and int is a subtype of Comparable).
  2. In 'copy', you first invoke 'iterable.iterator()', and since 'iterable' is Iterable<Comparable>, you get Iterator<Comparable>.
  3. Then, you create _Iterable<Comparable>, which is OK -- it's constructor expects Iterator<Comparable> and you pass Iterator<Comparable> (you don't define a type in the constructor, actually -- as per the latest spec, the type is taken from the field in such situation, but this isn't currently implemented, AFAIK, so this check isn't currently performed).
  4. You return the _Iterable<Comparable> as Iterable<Comparable>, which is OK.
  5. Then, in 'main', you try to assign Iterable<Comparable> (returned from 'copy') to Iterable<int>, which fails. Remember -- you could assign Iterable<int> (or, in fact, List<int>) to Iterable<Comparable>, but you can't do it otherwise. Iterable<Comparable> isn't a subtype of Iterable<int> (generics are covariant, not contravariant).

@kasperl
Copy link

kasperl commented Apr 16, 2012

Let's take this discussion to the [email protected] mailing list. Feel free to re-open the issue if you feel like this really is a bug in Dart.


Added Invalid label.

@DartBot DartBot added Type-Defect closed-invalid Closed as we don't believe the reported issue is generally actionable labels Apr 16, 2012
dart-bot pushed a commit that referenced this issue Aug 11, 2020
> git log --oneline 04b054b62cc437cf23451785fdc50e49cd9de139..master
0d185a39 (HEAD -> master, origin/master, origin/HEAD) Push null-safety forwards to 2.11 (#2604)
56f9f27f Hide outdated --mode flag (#2603)
61ce6f81 Avoid double loop (#2605)
fa6e57d7 (disable_mixed_mode_validation) Fix outdated latest ordering (#2598)
6549e4aa Remove unused dependency from pubspec.yaml (#2592)
61543d07 Don't look for external package foo during testing (#2599)
590b448f Fixed license headers (#2595)
04e0601e Don't show entries for dev-dependencies in outdated --json --no-dev-dependencies (#2591)
8c3778c4 Configure GitHub move app (#2578)
eec7beca (top_level_command) Pass --(no-)sound-null-safety arg through to VM. (#2542)
152e4740 Warn about publishing in mixed mode (#2583)
0b7a3abe Removed april fools toys (#2325)
b74a5b73 Actually print hints (#2582)
8ec3a66d (pub2) Fix outdated --no-color (#2572)
7bb3d4e6 Use getSdkPath() in NullSafetyAnalysis (#2573)
3c578f24 Drop the "magic" package concept (#2577)
0e967ff0 Remove unused function (#2570)
988fefef Remove dependency overrides (#2568)

Change-Id: I58bf14234ed55bf9d825de60a40ded1d65281195
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/158003
Reviewed-by: Jonas Jensen <[email protected]>
Commit-Queue: Sigurd Meldgaard <[email protected]>
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-invalid Closed as we don't believe the reported issue is generally actionable
Projects
None yet
Development

No branches or pull requests

2 participants