Casts as operator functions

Kotlin explicitly decided against smart casting so option 1 is out. Option 3 would need a different keyword since to is already an established function to create Pairs for maps, etc.

That leaves option 2 which will also not work. Kotlin has an established meaning behind foo.asX() and foo.toX(). The first is a cast/creates a wrapper object. If you change foo you also change the result of asX() and vice versa. toX() on the other hand creates a new object with an independent state. Therefor using the as operator for those functions is a bad idea.

3 Likes