Question about pattern matching


#1

Hello,

there is this sample code in the docs:

when (term) {
  is Fun -> { if (tem.v != term.body) continue; print(x) }
  else -> print("Nope!")
}

Admittedly, I don’t really understand the code. I could grasp it if it means something like this (this might not be the smartest Kotlin code as I have not played that much with Kotlin):

public class Fun(x_: Int, y_: Int)
{
    val x = x_
    val y = y_
}

val term = Fun(1, 3)

when (term) {
  is Fun -> {
          if(term.x != term.y) {
           continue
          }
          else
           println(term.x)
  }
  else -> print(“Nope!”)
}


Question is whether I’m right with my guess. If so, the problem is that the code above does not compile (am using Kotlin 0.9.74). The compiler complains that continue were not allowed there. Is there a way to work around this?

Thanks, Oliver


#2

Hello Oliver,

Yes, you get the sample right. Sadly, “continue” in “when” is not supported yet, see KT-771.

P. S. “tem” typo will be fixed :slight_smile: