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

Kotlin 1.1 tour

Kotlin 1.1 tour

きりみん

April 02, 2017
Tweet

More Decks by きりみん

Other Decks in Programming

Transcript

  1. ͨͷ͍͠Kotlin1.1 10෼πΞʔ @kirimin

  2. None
  3. What's New in Kotlin 1.1? https://kotlinlang.org/docs/reference/whatsnew11.html

  4. Bound callable references

  5. Bound callable references 1.0·Ͱ͸ؔ਺΍Ҿ਺ࣗ਎ͷϝιουΛؔ਺ࢀরͱͯ͠࢖༻ग़དྷ·͠ ͕ͨɺ1.1Ͱ͸ଞͷม਺ͷϝιουΛؔ਺ࢀরͱͯ͠࢖༻Ͱ͖ΔΑ͏ ʹͳΓ·ͨ͠ɻ

  6. Bound callable references val numbers = listOf("abc", "123", "456")
 


    // 1.1Ͱग़དྷΔΑ͏ʹͳͬͨ͜ͱɹଞͷม਺ͷؔ਺ࢀর
 val numberRegex = "\\d+".toRegex()
 numbers.filter(numberRegex::matches)
 // 1.0Ͱ΋ग़དྷͨ͜ͱୡ fun isAbc(str: String) = str.equals("abc", true)
 numbers.filter(::isAbc)
 numbers.map(String::toUpperCase)
 numbers.filter { numberRegex.matches(it) }
  7. Sealed and data classes

  8. Sealed and data classes SealedΫϥεͷαϒΫϥε͸SealedΫϥεͷΠϯφʔΫϥεͱͯ͠ ͚ͩͰ͸ͳ͘ɺಉҰϑΝΠϧ಺ʹฒྻʹఆٛ͢Δ͜ͱ΋ग़དྷΔΑ͏ ʹͳΓ·ͨ͠ɻ ·ͨɺdataΫϥε͸ࠓ·Ͱ͸ଞͷΫϥεΛܧঝ͢Δ͜ͱ͕ग़དྷ·ͤΜ Ͱ͕ͨ͠ɺ͜Ε΋ՄೳʹͳΓ·ͨ͠ɻ

  9. Sealed and data classes sealed class Express
 
 // ExpressͷαϒΫϥεΛExpressͱฒྻʹએݴ


    class Const(val number: Double) : Express()
 
 // dataΫϥε͸ଞͷΫϥεΛܧঝ͕Մೳʹ // sealedΫϥεͷαϒΫϥεʹ΋ग़དྷ·͢
 data class Sum(val e1: Express, val e2: Express) : Express()
  10. Destructuring Declarations

  11. Destructuring Declarations Destructuring Declarations(෼ղએݴ)͕ϥϜμࣜͷҾ਺Ͱ΋࢖༻ग़དྷΔ Α͏ʹͳΓ·ͨ͠ɻ KotlinͰ͸Pair΍MapͳͲΛval (a, b) = pairͷΑ͏ʹෳ਺ͷม਺ͱͯ͠

    એݴ͢Δࣄ͕ग़དྷ·͕͢ɺ1.1Ͱ͸ϥϜμͷҾ਺Λ௚઀(a, b)ͷΑ͏ͳ෼ ׂͨ͠ܗͰهड़͢Δࣄ͕ग़དྷΔΑ͏ʹͳΓ·ͨ͠ɻ
  12. Destructuring Declarations val map = mapOf(1 to "one", 2 to

    "two")
 // ࠓ·Ͱ͸Ҿ਺ΛҰ౓ड͚͔ͯΒ෼ׂએݴ͢Δඞཁ͕͋ͬͨ
 map.map { entry ->
 val (key, value) = entry
 "$key -> $value!"
 }
 
 // 1.1Ͱ͸௚઀keyͱvalueΛ෼ׂએݴ͢Δࣄ͕Մೳʹ map.map { (key, value) -> "$key -> $value!" }
  13. Underscores for unused parameters

  14. Underscores for unused parameters ϥϜμͰ࢖༻͠ͳ͍Ҿ਺ΛΞϯμʔείΞͰ໌ࣔ͢Δࣄ͕ग़དྷΔΑ͏ʹ ͳΓ·ͨ͠ɻ

  15. Underscores for unused parameters // key͸࢖༻͠ͳ͍ map.map { (_, value)

    -> "$value!" }
  16. Underscores in numeric literals

  17. Underscores in numeric literals KotlinͰ΋JavaͷΑ͏ʹ਺஋ϦςϥϧʹΞϯμʔείΞΛڬΜͰ ಡΈ΍͘͢ग़དྷΔΑ͏ʹͳΓ·ͨ͠ɻ

  18. Underscores in numeric literals val oneMillion = 1_000_000
 val hexBytes

    = 0xFF_EC_DE_5E
 val bytes = 0b11010010_01101001_10010100_10010010
  19. Shorter syntax for properties

  20. Shorter syntax for properties ϓϩύςΟͷܕΛgetter͔Βਪ࿦ͯ͠লུग़དྷΔΑ͏ʹͳΓ·͠ ͨɻ

  21. Shorter syntax for properties data class Person(val name: String, val

    age: Int) {
 // 1.1Ͱग़དྷΔΑ͏ʹͳͬͨ͜ͱ
 val isAdult get() = age >= 20 // Property type inferred to be 'Boolean'
 
 // 1.0·Ͱ͸ܕએݴ͕ඞཁͩͬͨ
 val isChild : Boolean get() = age < 20
 
 // ͜Ε͸1.0Ͱ΋ਪ࿦ͯ͘͠Εͨ
 val isBaby = age < 5
 }
  22. Inline property accessors

  23. Inline property accessors ϓϩύςΟͷΞΫηαʹ΋inline͕࢖༻ग़དྷΔΑ͏ʹͳΓ·ͨ͠ɻ ϓϩύςΟࣗମʹinlineΛ෇͚ͯ྆ΞΫηαʹinlineΛࢦఆ͢Δࣄ ΋ग़དྷ·͢ɻ

  24. Inline property accessors val <T> List<T>.lastIndex: Int
 inline get() =

    this.size - 1 inline val <T> List<T>.lastIndex: Int
 get() = this.size - 1
  25. Local delegated properties

  26. Local delegated properties ϩʔΧϧม਺ʹ΋by lazyͳͲͷdelegeted property͕࢖༻ग़དྷ ΔΑ͏ʹͳΓ·ͨ͠ɻ

  27. Local delegated properties val answer by lazy {
 println("Calculating the

    answer...")
 1 + 1
 }
 if (needAnswer()) { // answer͸͜͜ͰॳΊͯܭࢉ͞ΕΔ
 println("The answer is $answer.") 
 } else { // ͜ͷ৔߹͸answer͸ܭࢉ͞Εͳ͍
 println("Sometimes no answer is the answer...")
 }
  28. Generic enum value access

  29. Generic enum value access EnumΛδΣωϦΫεܕͱͯ͠ѻ͑ΔΑ͏ʹͳΓ·ͨ͠ɻ

  30. Generic enum value access enum class RGB { RED, GREEN,

    BLUE }
 
 // EnumΛδΣωϦΫεͱͯ͠डऔΓશͯͷ஋ͷnameΛग़ྗ͢Δؔ਺
 inline fun <reified T : Enum<T>> printAllValues() {
 print(enumValues<T>().joinToString { it.name })
 }
 
 fun printAllRGBColor() {
 printAllValues<RGB>()
 }
  31. ͦͷଞͷมߋɾৄࡉ͸ ެࣜυΩϡϝϯτͰ https://kotlinlang.org/docs/reference/whatsnew11.html