.as?(....)


#1

Hello.

I’m typing a lot of   ( b?.c()?.d() as? C)?.doSomething() commands

I would like to type something along b?.c()?.d().as?©?.doSomething()  to be able to include the safe cast to C in a command pipeline instead

Is this possible ? I think that it might require some syntax support at the language level, right ? (as C is a class name)

Olivier


#2

Here's an example of implementing such a cast as an extension function:

http://twitter.com/home?status=%23Solved%20CastFunction%20http%3A//kotlin-demo.jetbrains.com/%3FpublicLink%3D104074971561017308771-19941972%20%23Kotlin

http://kotlin-demo.jetbrains.com/?publicLink=104074971561017308771-19941972

[suppress(“UNCHECKED_CAST”)]
fun <R: Any> Any?.cast(): R? = this as R?