Slide 1

Slide 1 text

Kotlin Null-Safetyػߏʹ͍ͭͯ 2013-11-09 JJUG CCC 2013 fall JVMݴޠύωϧσΟεΧογϣϯ ! ೔ຊKotlinϢʔβάϧʔϓ
 ௕ᖒ ଠ࿠

Slide 2

Slide 2 text

Α͋͘Δᷤ౻ • NullPointerException͕සൃ • NullνΣοΫ • ඞཁѱ

Slide 3

Slide 3 text

Nullͱ্खʹ෇͖߹͏ํ๏ • ϝιουγάωνϟͷ޻෉ • ੩తղੳπʔϧ • ܕΛ࡞ͬͯϥοϓ

Slide 4

Slide 4 text

ϝιουγάωνϟͷ޻෉ calc(String s) throws NPE! getNameOrNull()

Slide 5

Slide 5 text

੩తղੳπʔϧ calc(@NotNull String s)! @NotNull String getName()

Slide 6

Slide 6 text

ܕΛ࡞ͬͯϥοϓ calc(Optional s)! Optional getName()

Slide 7

Slide 7 text

͵ΔΆ͸ۦஞͰ͖ͳ͍ calc(null);

Slide 8

Slide 8 text

ͦ͜ͰKotlinͷ! Null-Safety

Slide 9

Slide 9 text

Null-Safety • NotNull, NullableΛݴޠͰαϙʔτ • Φʔόϔουͳ͠ • Nullableʹ͸ͱ͜ͱΜ৻ॏʹͳΔ • Ͱ΋ॻ͖΍͢͞ɺಡΈ΍͢͞Λҡ࣋

Slide 10

Slide 10 text

NotNull val name: String

Slide 11

Slide 11 text

NotNull val name: String = null

Slide 12

Slide 12 text

NotNull val name: String = null ίϯύΠϧΤϥʔʂ! ࣮ߦՄೳͳόΠτίʔυΛੜ·ͳ͍! ৗʹ҆৺ͯ͠ࢀরͰ͖Δ

Slide 13

Slide 13 text

NotNull val name: String = null ίϯύΠϧΤϥʔʂ! ࣮ߦՄೳͳόΠτίʔυΛੜ·ͳ͍! ৗʹ҆৺ͯ͠ࢀরͰ͖Δ Null can not be a value of a non-null type jet.String

Slide 14

Slide 14 text

Nullable val name: String? = null! name.toUpperCase()

Slide 15

Slide 15 text

Nullable val name: String? = null! name.toUpperCase() ίϯύΠϧΤϥʔʂ! ෆ஫ҙʹΑΔNPEΛഉআ͢Δ

Slide 16

Slide 16 text

NullνΣοΫٛ຿Խ val name: String? = null! if(name != null)! ! name.toUpperCase() ίϯύΠϧ௨Δ

Slide 17

Slide 17 text

Safe Call ↓! if(name != null) name.toUpperCase()! else null val name: String? = null! name?.toUpperCase()

Slide 18

Slide 18 text

Safe Call͸ศར val got = listOf(“foo”, null) map{! it?.toUpperCase()?.reverse()! }! ! assert(got == listOf(“OOF”, null))

Slide 19

Slide 19 text

Safe Callͱ૬ੑͷྑ͍ػೳ val name =! (param[“name”] as? String)?.trim()! ! person.setName(name ?: “໊ແ͠”) Safe Cast Elvis operator

Slide 20

Slide 20 text

NPE͕ൃੜ͢Δ৔໘ • throw NullPointerException()! • ֎෦Javaίʔυͷݺͼग़͠ • !!ԋࢉࢠͷ࢖༻

Slide 21

Slide 21 text

͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠