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

2017_12_18-19_Kotlin_Conf_Report_Kotlin_Types_U...

AAkira
December 18, 2017

 2017_12_18-19_Kotlin_Conf_Report_Kotlin_Types_Updated

CA.kt #5, 6 Kotlin Conf Report in Osaka, Fukuoka
Kotlin Types
This presentation based on Kotlin Exposed Type by Svetlana Isakova in Kotlin Conf 2017.

https://cyberagent.connpass.com/event/73540/
https://cyberagent.connpass.com/event/73541/

AAkira

December 18, 2017
Tweet

More Decks by AAkira

Other Decks in Technology

Transcript

  1. @_a_akira AAkira CyberAgent, Inc. Akira Aratani private lateinit var aakira

    : User data class User(val name: String, val twitterId: String, val githubId: String, val company: String) print("Name : ${aakira.name}”) println("Github Id : ${aakira.githubId}") print("Twitter Id : ${aakira.twitterId}") println("Company : ${aakira.company}") $ whois
  2. 私とKotlin M1 2012-04-12 M11 2015-03-19 M14 2015-10-01 1.0-beta4 2015-12-22 M13

    2015-09-16 1.0 2016-02-16 1.0-RC 2016-02-04 2016-01-21 Release 2015-04 開発開始 kotlin FRESH 1.1 2017-03-01 2017-05-15 Renewal
  3. Agenda • Basic types • Nullable types • Collection types

    • Kotlin/Java types • Any, Unit, Nothing, void • Collections →
  4. Kotlin, Java Types public static final int foo() { return

    1; } @Nullable public static final Integer bar() { return Integer.valueOf(1); }
  5. Kotlin, Java Types public static final int foo() { return

    1; } @Nullable public static final Integer bar() { return Integer.valueOf(1); }
  6. Any, Unit, Nothing, void The root of the Kotlin class

    hierarchy. Every Kotlin class has [Any] as a superclass. Any
  7. Any, Unit, Nothing, void log(2017) fun log(any: Any) { println("Value:

    $any") } fun log(i: Int) { println("Value: $i”) }
  8. Any, Unit, Nothing, void log(2017) fun log(any: Any) { println("Value:

    $any") } fun log(i: Int) { println("Value: $i”) } Auto boxing
  9. val hoge: Int? = 2017 log(hoge) fun log(any: Any) {

    println("Value: $any") } Any, Unit, Nothing, void
  10. val hoge: Int? = 2017 log(hoge) fun log(any: Any) {

    println("Value: $any") } Any, Unit, Nothing, void ⇨ Compile Error
  11. Any, Unit, Nothing, void Unit The type with only one

    value: the Unit object. This type corresponds to the `void` type in Java.
  12. Any, Unit, Nothing, void Nothing has no instances. You can

    use Nothing to represent "a value that never exists”: for example, if a function has the return type of Nothing,
 it means that it never returns (always throws an exception). Nothing
  13. Any, Unit, Nothing, void val foo = if (bar) {

    123 } else { fail() } fun fail() { throw IllegalStateException() }
  14. Any, Unit, Nothing, void val foo = if (bar) {

    123 } else { fail() } fun fail(): Unit { throw IllegalStateException() }
  15. Any, Unit, Nothing, void val foo = if (bar) {

    123 } else { fail() } fun fail(): Unit { throw IllegalStateException() }
  16. Any, Unit, Nothing, void val foo = if (bar) {

    123 } else { fail() } fun fail(): Unit { throw IllegalStateException() } Int Unit
  17. Any, Unit, Nothing, void val foo: Any = if (bar)

    { 123 } else { fail() } fun fail(): Unit { throw IllegalStateException() }
  18. Any, Unit, Nothing, void val foo: Any = if (bar)

    { 123 } else { fail() } fun fail(): Unit { throw IllegalStateException() } ⇨ fooはInt型にしたい
  19. Any, Unit, Nothing, void val foo = if (bar) {

    123 } else { fail() } fun fail(): Nothing { throw IllegalStateException() }
  20. Any, Unit, Nothing, void val foo = if (bar) {

    123 } else { fail() } fun fail(): Nothing { throw IllegalStateException() } Int Nothing
  21. Any, Unit, Nothing, void val foo: Int = if (bar)

    { 123 } else { fail() } fun fail(): Nothing { throw IllegalStateException() }
  22. Any, Unit, Nothing, void val foo: Int = if (bar)

    { 123 } else { fail() } fun fail(): Nothing { throw IllegalStateException() }
  23. Collections val mutableList = mutableListOf(1, 2, 3) val list: List<Int>

    = mutableList println(list) // [1, 2, 3] mutableList.add(4) println(list)
  24. Collections val mutableList = mutableListOf(1, 2, 3) val list: List<Int>

    = mutableList println(list) // [1, 2, 3] mutableList.add(4) println(list) // [1, 2, 3, 4]
  25. Collections @NotNull public static final List foo() { return CollectionsKt.listOf(Integer.valueOf(1));

    } @NotNull public static final List bar() { return CollectionsKt.mutableListOf(
 new Integer[]{Integer.valueOf(1)}); }
  26. Collections @NotNull public static final List foo() { return CollectionsKt.listOf(Integer.valueOf(1));

    } @NotNull public static final List bar() { return CollectionsKt.mutableListOf(
 new Integer[]{Integer.valueOf(1)}); }