Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

Bound callable references

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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) }

Slide 7

Slide 7 text

Sealed and data classes

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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()

Slide 10

Slide 10 text

Destructuring Declarations

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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!" }

Slide 13

Slide 13 text

Underscores for unused parameters

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

Underscores for unused parameters // key͸࢖༻͠ͳ͍ map.map { (_, value) -> "$value!" }

Slide 16

Slide 16 text

Underscores in numeric literals

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

Underscores in numeric literals val oneMillion = 1_000_000
 val hexBytes = 0xFF_EC_DE_5E
 val bytes = 0b11010010_01101001_10010100_10010010

Slide 19

Slide 19 text

Shorter syntax for properties

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

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
 }

Slide 22

Slide 22 text

Inline property accessors

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

Inline property accessors val List.lastIndex: Int
 inline get() = this.size - 1 inline val List.lastIndex: Int
 get() = this.size - 1

Slide 25

Slide 25 text

Local delegated properties

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

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...")
 }

Slide 28

Slide 28 text

Generic enum value access

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

Generic enum value access enum class RGB { RED, GREEN, BLUE }
 
 // EnumΛδΣωϦΫεͱͯ͠डऔΓશͯͷ஋ͷnameΛग़ྗ͢Δؔ਺
 inline fun > printAllValues() {
 print(enumValues().joinToString { it.name })
 }
 
 fun printAllRGBColor() {
 printAllValues()
 }

Slide 31

Slide 31 text

ͦͷଞͷมߋɾৄࡉ͸ ެࣜυΩϡϝϯτͰ https://kotlinlang.org/docs/reference/whatsnew11.html