-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path19-reification.kt
29 lines (21 loc) · 963 Bytes
/
19-reification.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package org.athenian
abstract class Book(val name: String)
class Fiction(name: String) : Book(name)
class NonFiction(name: String) : Book(name)
fun <T : Book> findFirstWithoutReification(books: List<Book>, clazz: Class<T>): T =
(books
.filter { it.javaClass == clazz }
.firstOrNull() ?: throw RuntimeException("Not found")) as T
inline fun <reified T : Book> findFirstWithReification(books: List<Book>): T =
(books
.filter { it is T }
.firstOrNull() ?: throw RuntimeException("Not found")) as T
fun main() {
val books = listOf(Fiction("True Grit"), NonFiction("Kotlin in Action"), Fiction("Hawaii"))
println("Without reification:")
println(findFirstWithoutReification(books, NonFiction::class.java).name)
println(findFirstWithoutReification(books, Fiction::class.java).name)
println("\nWith reification:")
println(findFirstWithReification<NonFiction>(books).name)
println(findFirstWithReification<Fiction>(books).name)
}