KotlinQuickReference icon indicating copy to clipboard operation
KotlinQuickReference copied to clipboard

`as` and `as?`

Open alvinj opened this issue 6 years ago • 1 comments

Need some coverage of as and as?, probably as a separate chapter (at least initially).

alvinj avatar Nov 23 '18 19:11 alvinj

The start of some code:

fun main(args: Array<String>) {

    val d = Dog()
    val c = Cat()

    val x = d as Dog

    //ClassCastException: Cat cannot be cast to Dog
    val y = c as Dog

}

interface Pet
class Dog : Pet
class Cat : Pet

alvinj avatar Nov 23 '18 23:11 alvinj