Chachako
12/29/2021, 3:00 PMSmartCastManager
at the compiler-frontend, but I don’t know how to start. I want to smart cast type after an expression, for example:
class CallBlock(private val types: KClass<*>) {
fun resultAt(index: Int): Any = results[index]
}
fun call(vararg types: KClass<*>, block: CallBlock.() -> Unit) = ...
call(Int::class, String::class) {
val a = resultAt(0) // Implicit: Int
val b = resultAt(1) // Implicit: String
}
---
As shown in the code, I want to smart cast the type result of resultAt()
callChachako
12/29/2021, 4:16 PMTypeResolutionInterceptorExtension
, will it help me reach there?dmitriy.novozhilov
12/30/2021, 10:23 AMinline <T> fun runIf(condition: Boolean, block: () -> T): T? contract [
condition trueIn block // not final syntax
] {
return if (condition) block() else null
}
fun test(cond: Boolean, x: Any) {
val y = runIf(x is String) {
x.length // x smartcasted to String
}
}
Chachako
12/30/2021, 10:34 AMChachako
12/30/2021, 10:38 AMclass CallBlock(private val types: KClass<*>) {
/** synthetic */ val result0: Int = ...
/** synthetic */ val result1: String = ...
}
call(Int::class, String::class) {
val a = result0 // Type: Int
val b = result1 // Type: String
}
But I don’t know if this is possible, because it seems to need to create a separate class of CallBlock
for each call
, so I hope there is a more friendly way to solve this problem 🥲dmitriy.novozhilov
01/03/2022, 10:49 AMWill it appear with the stability of FIR?Yes, it's the plan
In addition, maybe my needs can be solved in another way, such asYou probably can do it, but I'm not sure that in FIR there will be similar extension pointSyntheticResolveExtension