July 2018
Intermediate to advanced
400 pages
12h 14m
English
There are cases where it is useful to know the specific type that is used for a generic parameter.
The reified keyword allows you to check a generic parameter’s type.
Imagine that you wanted to fetch loot from a list of different kinds of loot (Coins and Fedoras, for example), and – depending on the type of loot that was randomly selected – you either wanted to provide a backup loot item of a desired type or return the one that was selected. Here is a randomOrBackupLoot function that attempts to capture that logic:
fun <T> randomOrBackupLoot(backupLoot: () -> T): T { val items = listOf(Coin(14), Fedora("a fedora of the ages", 150)) val randomLoot: Loot = items.shuffled().first() return if ...Read now
Unlock full access