magiepooh
June 14, 2017
1.5k

# Kotlin入門

スコープ関数の理解をゴールとしたKotlinの入門資料

June 14, 2017

## Transcript

Kotlinೖ໳
@magie_pooh
αϙʔλʔζKotlinษڧձ #3

2. ࣗݾ঺հ
౻ాୖຏ
@magie_pooh
@magiepooh

3. FOLIO
γϦʔζAϥ΢ϯυͰ18ԯԁͷࢿۚௐୡΛ࣮ࢪ͠·͠
ͨɻ೔ຊॳͷςʔϚ౤ࢿܕΦϯϥΠϯূ݊Λͭ͘Γ·͢ɻ

4. What’s Kotlin?

5. Kotlin
• Jet Brains ͕࡞ͬͨJVMݴޠ
• ੩తܕ෇͚ͷΦϒδΣΫτࢦ޲ݴޠ
• 2012೥ࠒ M1ϦϦʔε
• 2016೥ 1.0 ϦϦʔε
• Support JS(Kotlin/JS)
• LLVM(Kotlin/Native)

6. Kotlin
• Lambdas
• Nullable(NonNull)
• High-Order Functions(Stream)
• Extension Functions

7. Kotlin
• Lambdas -> Retrolambda
• Nullable(NonNull) -> Optional
• High-Order Functions(Stream) -> StremeAPI,
RxJava
• Extension Functions

8. Kotlin
• Lambdas -> Retrolambda
• Nullable(NonNull) -> Optional
• High-Order Functions(Stream) -> StremeAPI,
RxJava
• Extension Functions

9. ࠓ೔ͷΰʔϧ
• είʔϓؔ਺ͷཧղ
• apply
• let
• run
• with

10. ม਺

11. val num : Int = 1
var num2 : Int = 2

num = 3 // error
num2 = 3
ม਺ͷએݴ

12. val num = 1
var num2 = 2

num = 3 // error
num2 = 3
ม਺ͷએݴ

13. Null҆શ
var a: String = "abc"
a = null // => ίϯύΠϧΤϥʔ

var b: String? = "abc"
b = null // => OK

14. Null҆શʢݺͼग़͠ʣ
var b: String? = "abc"
val l : Int = b.length // => ίϯύΠϧΤϥʔ

var b: String? = "abc"
val l : Int = if(b != null) {
b.length
} else {
-1
}

val l : Int? = b?.length
val l : Int = b?.length ?: -1

15. Null҆શʢΤϧϏεԋࢉࢠʣ
var b: String? = "abc"
val l : Int = if(b != null)
b.length else -1
val l : Int = b?.length ?: -1

16. ؔ਺

17. ؔ਺ͷجຊ
fun add(x: Int, y: Int): Int {
return x + y
}

18. ؔ਺ͷجຊ
fun add(x: Int, y: Int): Int = x + y

19. ؔ਺ͷجຊ
fun add(x: Int, y: Int) = x + y

20. ߴ֊ؔ਺

21. ߴ֊ؔ਺
// body ͱ͍͏໊લͷҾ਺ʹؔ਺ΦϒδΣΫτΛ౉͍ͯ͠Δ
fun lock(lock: Lock, body: () -> T): T {
lock.lock()
try {
// Ҿ਺Ͱ౉͞Εͨؔ਺ΦϒδΣΫτΛ࣮ߦ͍ͯ͠Δ
return body()
} finally {
lock.unlock()
}
}

22. ߴ֊ؔ਺
// body ͱ͍͏໊લͷҾ਺ʹؔ਺ΦϒδΣΫτΛ౉͍ͯ͠Δ
fun lock(lock: Lock, body: () -> T): T {
lock.lock()
try {
// Ҿ਺Ͱ౉͞Εͨؔ਺ΦϒδΣΫτΛ࣮ߦ͍ͯ͠Δ
return body()
} finally {
lock.unlock()
}
}

23. ϥϜμࣜ

24. ϥϜμࣜ
max(strings, { a, b -> a.length < b.length })
max(strings) { a, b -> a.length < b.length }

25. ֦ுؔ਺

26. ֦ுؔ਺
"hoge".isEmpty()

27. ֦ுؔ਺
"hoge".isEmpty()
@kotlin.internal.InlineOnly
public inline fun CharSequence.isEmpty():
Boolean = length == 0

28. ֦ுؔ਺
"hoge".isEmpty()
@kotlin.internal.InlineOnly
public inline fun CharSequence.isEmpty():
Boolean = length == 0
Ϩγʔό

29. ֦ுؔ਺
"hoge".isEmpty()
@kotlin.internal.InlineOnly
public inline fun CharSequence.isEmpty():
Boolean = length == 0
this.length:
this ͸ϨγʔόΦϒδΣΫτʹରԠ

30. ࿅श໰୊Ͱߟ͑ͯΈΔ

31. public String toJSON(Collection
collection) {
StringBuilder sb = new StringBuilder();
sb.append("[");
Iterator iterator =
collection.iterator();
while (iterator.hasNext()) {
Integer element = iterator.next();
sb.append(element);
if (iterator.hasNext()) {
sb.append(", ");
}
}
sb.append("]");
return sb.toString();
}

32. ౴͑

33. fun toJSON(collection: Collection):
String {
val sb = StringBuilder()
sb.append("[")
val iterator = collection.iterator()
while (iterator.hasNext()) {
val element = iterator.next()
sb.append(element)
if (iterator.hasNext()) {
sb.append(", ")
}
}
sb.append("]")
return sb.toString()
}

34. public String toJSON(Collection
collection) {
StringBuilder sb = new StringBuilder();
sb.append("[");
Iterator iterator =
collection.iterator();
while (iterator.hasNext()) {
Integer element = iterator.next();
sb.append(element);
if (iterator.hasNext()) {
sb.append(", ");
}
}
sb.append("]");
return sb.toString();
}

35. είʔϓؔ਺

36. είʔϓؔ਺ୡ(ൈਮ)
public inline fun T.run(block: T.() -> R): R = block()
public inline fun with(receiver: T, block: T.() -> R): R =
public inline fun T.apply(block: T.() -> Unit): T { block();
return this }
public inline fun T.also(block: (T) -> Unit): T { block(this);
return this }
public inline fun T.let(block: (T) -> R): R = block(this)

37. είʔϓؔ਺(run)
public inline fun T.run(block: T.() -> R): R = block()
val s = "hoge".run { toUpperCase() }
println(s) //=> HOGE
೚ҙͷܕTΛϨγʔόͱ͠ɺ
೚ҙͷܕRΛฦؔ͢਺

38. είʔϓؔ਺(run)
public inline fun T.run(block: T.() -> R): R = block()
val s = "hoge".run { toUpperCase() }
println(s) //=> HOGE
ϨγʔόΦϒδΣΫτʹ͸thisͰΞΫηεՄೳ
ʢলུ͍ͯ͠Δʣ

39. είʔϓؔ਺(with)
public inline fun with(receiver: T, block: T.() -> R): R =
val s = with("hoge") { this.toUpperCase() }
println(s) //=> HOGE

40. είʔϓؔ਺(with)
public inline fun with(receiver: T, block: T.() -> R): R =
val s = with("hoge") { toUpperCase() }
println(s) //=> HOGE
֦ுؔ਺Ͱ͸ͳ͍

41. είʔϓؔ਺(with)
public inline fun with(receiver: T, block: T.() -> R): R =
val s = with("hoge") { toUpperCase() }
println(s) //=> HOGE
ϨγʔόΦϒδΣΫτʹ͸thisͰΞΫηεՄೳ
ʢলུ͍ͯ͠Δʣ

42. είʔϓؔ਺(apply)
public inline fun T.apply(block: T.() -> Unit): T { block();
return this }
val s = "hoge".apply { toUpperCase() }
println(s) //=> hoge
໭Γ஋͸this
ͭ·ΓɺϨγʔόΦϒδΣΫτࣗ਎

43. είʔϓؔ਺(apply)
public inline fun T.apply(block: T.() -> Unit): T { block();
return this }
val s = "hoge".apply { toUpperCase() }
println(s) //=> hoge
toUpperCase()Λ͍ͯ͠Δ͕ɺ
“hoge” ͕ฦ͞ΕΔͷͰ஫ҙ

44. είʔϓؔ਺(apply)
view.layoutParams = view.layoutParams.apply { height = width / 2 }

45. είʔϓؔ਺(also)
public inline fun T.also(block: (T) -> Unit): T { block(this);
return this }
val s = "hoge".also { it.toUpperCase() }
println(s) //=> hoge
໭Γ஋͸this
ͭ·ΓɺϨγʔόΦϒδΣΫτࣗ਎

46. είʔϓؔ਺(also)
public inline fun T.also(block: (T) -> Unit): T { block(this);
return this }
val s = "hoge".also { it.toUpperCase() }
println(s) //=> hoge
Ҿ਺ͱͯ͠T͕౉ͬͯ͘ΔͨΊɺ
itͰΞΫηεՄೳ

47. είʔϓؔ਺(let)
public inline fun T.let(block: (T) -> R): R = block(this)
val upperCase: String? = foo?.let { it.toUpperCase() }

48. είʔϓؔ਺Λ༻͍ͯɺ
ઌఔͷ໰୊Λ͞Βʹ؆ܿʹɻ

49. fun toJSON(collection: Collection):
String {
val sb = StringBuilder()
sb.append("[")
val iterator = collection.iterator()
while (iterator.hasNext()) {
val element = iterator.next()
sb.append(element)
if (iterator.hasNext()) {
sb.append(", ")
}
}
sb.append("]")
return sb.toString()
}

50. fun toJSON(collection: Collection):
String =
StringBuilder().run {
append("[")
collection.iterator().also {
while (it.hasNext()) {
append(it.next())
if (it.hasNext()) {
append(", ")
}
}
}
append("]")
toString()
}

51. ͜Ε͔Βͷֶश

52. Next Step
• Slack
• kotlinlang: http://slack.kotlinlang.org/
• kotlinlang-jp: http://kotlinlang-jp.herokuapp.com/
• Try Kotlin: https://try.kotlinlang.org/
• σίϯύΠϧ
• JS, Server: https://github.com/Kotlin/kotlin-fullstack-sample/
• ຊ
• ॿ૸ಡຊʢແྉެ։ͷPDFʣ: https://goo.gl/5vUT7o
• Kotlinʢ੺΂͜ຊʣ

53. ͜Ε͔Βͷֶश
• Slack
• kotlinlang: http://slack.kotlinlang.org/
• kotlinlang-jp: http://kotlinlang-jp.herokuapp.com/
• Try Kotlin: https://try.kotlinlang.org/
• σίϯύΠϧ
• JS, Server: https://github.com/Kotlin/kotlin-fullstack-sample/
• ຊ
• ॿ૸ಡຊʢແྉެ։ͷPDFʣ: https://goo.gl/5vUT7o
• Kotlinʢ੺΂͜ຊʣ