For context, the compiler inlines any `value is Va...
# compiler
y
For context, the compiler inlines any
value is ValueClass
checks and eliminates any branches that will never run as a result. This happens even in inline functions when
value
is of some type argument
T
. Is this optimization documented somewhere? because any other constant conditions never cause code to collapse like that. E.g.:
Copy code
public inline fun <T : Any> printlnWithoutBoxing(value: T) {
  // This `is` check gets inlined by the compiler if `value` is statically known to be Result
  println(if(value is Result<*>) (value as Result<*>).toString() else value)
}

public fun test(){
  val myResult = Result.success(42)
  val myResultAny: Any = myResult
  println(myResult)
  println(myResultAny)
}

public fun test2(){
  val myResult = Result.success(42)
  val myResultAny: Any = myResult
  printlnWithoutBoxing(myResult)
  printlnWithoutBoxing(myResultAny)
}
The else branch disappears for the calls in test2, even in the
myResultAny
case! In fact, I have to use
myResultAny
as a "real" Any (i.e. pass it to a non-inline function that expects Any or a generic
T
, or pass it to an inline function that uses that argument as a "real" Any from the aforementioned criteria). The behavior is surprising, but it is useful. Is this how the compiler is expected to behave? (Decompilation of example in 🧵 )