/** Point class with [Int] precision */
data class Point(val x: Int, val y: Int) : Comparable<Point> {
override fun compareTo(other: Point): Int {
val yComparison = y.compareTo(other.y)
if(yComparison != 0) return yComparison
return x.compareTo(other.x)
}
operator fun rangeTo(other: Point) = RectangleRange(this, other)
}
class RectangleRange(override val start: Point, override val endInclusive: Point) : ClosedRange<Point>, Iterable<Point> {
private val maxX = endInclusive.x
override fun iterator() = object : Iterator<Point> {
private var current = start
private operator fun Point.inc() = if(x == maxX) Point(0, y+1) else Point(x+1, y)
// getting "error: 'operator' modifier is inapplicable on this function: receiver must be a supertype of the return type"
override fun hasNext() = current <= endInclusive
override fun next(): Point {
if (!hasNext()) throw NoSuchElementException()
return current++
// Have to do return current.also { current = current.inc() } instead and remove the 'operator'
}
}
}
fun main(args : Array<String>){
for(x in Point(0, 0) .. Point(2, 2)) println(x)
}