dart-lang / dart-lang/language
No static error when iterating over a generic type, T.
- Dominant language
- TeX
- Stars
- 2.9k
- Forks
- 239
- Avg merge
- 2d 18h
- Merged PRs (30d)
- 14
Description
@matanlurey commented on [Thu May 30 2019](https://github.com/dart-lang/sdk/issues/37125)
```dart
void main() {
example1(5);
example2(3);
}
void example1(T item) {
// No static error.
// Runtime error (in Dart2JS):
// TypeError: 5: type 'JSInt' is not a subtype of type 'Iterable'
for (final x in item) {
print(x);
}
}
void example2(T item) {
// Static error: The method 'iterator' isn't defined for the class 'Object'.
final iterator = item.iterator();
while (iterator.moveNext()) {
print(iterator.current);
}
}
```
I'd expect the same or similar error in both cases.
/cc @leafpetersen
---
@vsmenon commented on [Thu May 30 2019](https://github.com/dart-lang/sdk/issues/37125#issuecomment-497520142)
@leafpetersen @lrhn - is this a language issue or an implementation one? Right now, it looks like none of the implementations report a static error for the `for`.
Contributor guide
Research direction
Start with the Dart examples in the issue and compare the analyzer's handling of `for (final x in item)` with the explicit `item.iterator()` call. Review the language specification and implementation behavior to determine whether both cases should produce a static error. Done means the intended behavior is specified and the relevant implementations consistently enforce it.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- dart
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100