Assignment not allow in while expression?

A scenario where assignment is useful as an expression is one like:

class IntRange(public final val start: Int, public final val end: Int) : Iterable<Int> {   override fun iterator(): Iterator<Int> = IntIterator(this) }

class IntIterator(final val range: IntRange) : Iterator<Int> {
  private var current: Int = range.start
  override fun next(): Int = (current = current + 1) // this does not compile because an assignment isn’t an expression that returns the value assigned like in many other languages
  override fun hasNext(): Int = current <= range.end
}


This is a contrived case as an example, but the point is that having an assignment evaluate to the value of the assignment allows the programmer to assign a value and return it without having to assign and return as separate steps.

1 Like