Upgrade to Pro — share decks privately, control downloads, hide ads and more …

略解reified

scache
December 19, 2017

 略解reified

Kotlinのreifiedがどのようにして型引数の型情報を取得できるようにしているのかについての解説

scache

December 19, 2017
Tweet

More Decks by scache

Other Decks in Programming

Transcript

  1. ܕҾ਺ͷࢀর fun <T> isA(obj: Any) = obj is T inline

    fun <reified T> f(obj: Any) = obj is T
  2. ܕҾ਺ͷࢀর fun <T> isA(obj: Any) = obj is T inline

    fun <reified T> f(obj: Any) = obj is T inline fun <reified T> f() = T::class.java
  3. ܕҾ਺ͷࢀর fun <T> isA(obj: Any) = obj is T inline

    fun <reified T> f(obj: Any) = obj is T inline fun <reified T> f() = T::class.java
  4. inlineؔ਺ fun example() { val v1 = 3 println(noInlineSquare(v1)) }

    fun noInlineSquare(v: Int) = v * v println(noInlineSquare(v1))
  5. inlineؔ਺ fun example() { val v1 = 3 println(noInlineSquare(v1)) val

    v2 = 3 println(inlineSquare(v2)) } fun noInlineSquare(v: Int) = v * v inline fun inlineSquare(v: Int) = v * v println(noInlineSquare(v1))
  6. inlineؔ਺ fun example() { val v1 = 3 println(noInlineSquare(v1)) val

    v2 = 3 println(inlineSquare(v2)) } fun noInlineSquare(v: Int) = v * v inline fun inlineSquare(v: Int) = v * v println(noInlineSquare(v1)) println(v2 * v2)
  7. inlineؔ਺ fun example() { val v1 = 3 println(noInlineSquare(v1)) val

    v2 = 3 println(inlineSquare(v2)) } fun noInlineSquare(v: Int) = v * v inline fun inlineSquare(v: Int) = v * v println(v2 * v2) println(noInlineSquare(v1))
  8. reified inline fun <reified T> f(obj: Any) = obj is

    T fun testIs() { assert(f<String>("kotlin")) }
  9. reified inline fun <reified T> f(obj: Any) = obj is

    T fun testIs() { assert(f<String>("kotlin")) } fun testIs() { assert("kotlin" is T) }
  10. reified inline fun <reified T> f(obj: Any) = obj is

    T fun testIs() { assert(f<String>("kotlin")) } fun testIs() { assert("kotlin" is T) }
  11. reified inline fun <reified T> f(obj: Any) = obj is

    T fun testIs() { assert(f<String>("kotlin")) } fun testIs() { assert("kotlin" is T) } fun testIs() { assert("kotlin" is String) }