trait ISized { val size : Int } trait javaUtilIterator : java.util.Iterator { override fun remove() : Unit { throw UnsupportedOperationException() } } class MyIterator(val array : ReadOnlyArray) : javaUtilIterator { private var index = 0 override fun hasNext() : Boolean = index < array.size override fun next() : T = array.get(index++) } trait ReadOnlyArray : ISized, java.lang.Iterable { fun get(index : Int) : T class Default { fun check(v: Any) = v is T } override fun iterator() : java.util.Iterator = MyIterator(this) } trait WriteOnlyArray : ISized { fun set(index : Int, value : T) : Unit fun set(from: Int, count: Int, value: T) { for(i in from..from+count-1) { set(i, value) } } } class MutableArray(length: Int, init : (Int) -> T) : ReadOnlyArray, WriteOnlyArray { private val array = Array(length, init) override fun get(index : Int) : T = array[index] override fun set(index : Int, value : T) : Unit { array[index] = value } override val size : Int get() = array.size } fun box() : String { var a = MutableArray (4, {0}) a [0] = 10 a.set(1, 2, 13) a [3] = 40 System.out?.println(a.iterator()) System.out?.println(a.iterator().hasNext()) for(el in a) { System.out?.println(el) } return "OK" }