Strange iterator behavior


#1

 

package iterators

import kotlin.support.AbstractIterator
import java.util.Iterator

class MyIterator : AbstractIterator<String>() {
  var count : Int = 0
  public override fun computeNext() {
  if (count++ == 0) {
           setNext(“aa”)
  } else {
           done()
  }
  }
}

class MyIterable : java.lang.Iterable<String> {
  public override fun iterator() : Iterator<String> {
  return MyIterator()
  }

}

fun main(args : Array<String>) {
  for (val x in MyIterable()) {
  println(x)
  }
}


But this fails with runtime error

Exception in thread "main" java.lang.NoSuchMethodError: iterators.MyIterator.setNext(Ljava/lang/String;)V      at iterators.MyIterator.computeNext(iterators.kt:10)      at kotlin.support.AbstractIterator.tryToComputeNext(AbstractIterator.kt:47)      at kotlin.support.AbstractIterator.hasNext(AbstractIterator.kt:25)      at iterators.namespace.main(iterators.kt:25)      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)      at java.lang.reflect.Method.invoke(Method.java:597)      at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

It looks like something invalid has been generated by the compiler..


#2

O.. I found this can be reproduced with no MyIterable.. just iterate over MyIterator to get error.

Much shorter example:

package iterators

import kotlin.support.AbstractIterator

class MyIterator : AbstractIterator<String>() {
  public override fun computeNext() {
  setNext("")
  }

}
fun main(args : Array<String>) {
  for (x : String in MyIterator()) {
  println(x)
  }
}


#3

Looks like a bug. Mind to file it in the tracker? Thanks


#4

Done

http://youtrack.jetbrains.com/issue/KT-1779