Is there a way to smart cast the type of expressio...
# compiler
c
Is there a way to smart cast the type of expressions through the compiler plugin? I noticed a
SmartCastManager
at the compiler-frontend, but I don’t know how to start. I want to smart cast type after an expression, for example:
Copy code
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()
call
I found a
TypeResolutionInterceptorExtension
, will it help me reach there?
d
Right now it's impossible to provide new smartcasts via compiler plugin (and I don't think that we add such ability in future) But we want to provide new contracts to language which can make user defined smartcasts more flexible, like in this case:
Copy code
inline <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
    }
}
👀 1
c
Well, this is a pity, but I think it is also excellent to provide a powerful contracts feature. Will it appear with the stability of FIR?
In addition, maybe my needs can be solved in another way, such as `SyntheticResolveExtension`:
Copy code
class 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 🥲
d
Will it appear with the stability of FIR?
Yes, it's the plan
In addition, maybe my needs can be solved in another way, such as 
SyntheticResolveExtension
You probably can do it, but I'm not sure that in FIR there will be similar extension point