Skip to content

Failed type check: type Function is not assignable to type Iterator #2567

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

Failed type check: type Function is not assignable to type Iterator #2567

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?
I don't know what is the problem with the following program, as
similar pattern works in Java:

bool check(Iterable iter) {
  return iter !== null;
}

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

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

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

main() {
  Expect.isTrue(check(copy([0, 1])));
}

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

What is the expected output? What do you see instead?
Expected: program should run without a problem
Actual:
Dartboard: Failed type check: type Function is not assignable to type Iterator
Dart editor: Unhandled exception:
type '() => Iterator<Dynamic>' is not a subtype of type 'Iterator<Dynamic>' of '_iterator@28152909'.

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]


Iterable copy(Iterable iterable) {
return new _Iterable(iterable.iterator);

Here's the problem. You aren't calling the iterator function, you are closurizing it. If you write iterable.iterator, you get back a function that, when called, produces an iterator. Here, you want to produce the iterator immediately, so you must write iterable.iterator(). This isn't the first (and certainly not last) occurence of this "issue" (I personally call it the closurization trap).

Oh, and btw, creating an Iterable that is backed by an Iterator is a terrible idea, because you can iterate over it only once, yet you usually expect that you can iterate over an Iterable as many times as you want. Here's a reference why it wasn't added to Guava: http://code.google.com/p/guava-libraries/wiki/IdeaGraveyard#A_method_to_view_an_iterator_as_an_iterable

P.S.: I think that this would be better suited to the [email protected] mailing list than to the issue tracker.

@kasperl
Copy link

kasperl commented Apr 16, 2012

Not really an actionable issue for us. Marking as invalid.


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 Jul 20, 2020
git log --oneline 85bdb898aeb2ac7cea76489ba0bf4e52ff7a79bf..master
04b054b6 Find sdk path with cli_util (#2567)
36c195b9 Only alert about new prereleases for prereleases (#2556)
00b36394 Stop using deprecated mapMap from pkg:collection (#2544)
477cbe72 Improved expectations in flutter_plugin_format validator test (#2532)
a8e2442b `pub run` Don't write about precompilation if no terminal is attached (#2531)
d83d36a4 Don't precompile dependencies of global activate from path (#2530)
870f395c Sanitize git cache folder names (#2522)
03b7f9a5 Avoid divisive terms (#2523)
b239ada4 Remove divisive term (#2517)

Change-Id: I55ea52a86121a382dca5c0f85addd4513ada50bf
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/154940
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