Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Kotlinウォークスルー
Search
Taro Nagasawa
July 04, 2014
Programming
0
1.8k
Kotlinウォークスルー
第2回かわいいKotlin勉強会の発表資料です
Taro Nagasawa
July 04, 2014
Tweet
Share
More Decks by Taro Nagasawa
See All by Taro Nagasawa
Android開発者のための Kotlin Multiplatform入門
ntaro
0
790
Kotlin 最新動向2022 #tfcon #techfeed
ntaro
1
2.3k
#Ubie 狂気の認知施策と選考設計
ntaro
13
13k
UbieにおけるサーバサイドKotlin活用事例
ntaro
1
1.1k
KotlinでSpring 完全理解ガイド #jsug
ntaro
6
3.5k
Kotlinでサーバサイドを始めよう!
ntaro
1
1k
Androidからサーバーサイドまで!プログラミング言語 Kotlinの魅力 #devboost
ntaro
5
2.8k
Kotlin Contracts #m3kt
ntaro
4
4.2k
How_to_Test_Server-side_Kotlin.pdf
ntaro
1
520
Other Decks in Programming
See All in Programming
When Dependencies Fail: Building Antifragile Applications in a Fragile World
selcukusta
0
110
overlayPreferenceValue で実現する ピュア SwiftUI な AdMob ネイティブ広告
uhucream
0
200
理論と実務のギャップを超える
eycjur
0
170
ALL CODE BASE ARE BELONG TO STUDY
uzulla
26
6.6k
スキーマ駆動で、Zod OpenAPI Honoによる、API開発するために、Hono Takibiというライブラリを作っている
nakita628
0
280
Six and a half ridiculous things to do with Quarkus
hollycummins
0
200
Introduce Hono CLI
yusukebe
6
3k
オープンソースソフトウェアへの解像度🔬
utam0k
17
3.1k
TransformerからMCPまで(現代AIを理解するための羅針盤)
mickey_kubo
3
970
開発生産性を上げるための生成AI活用術
starfish719
3
1.6k
Catch Up: Go Style Guide Update
andpad
0
240
CSC509 Lecture 08
javiergs
PRO
0
230
Featured
See All Featured
The Art of Programming - Codeland 2020
erikaheidi
56
14k
Keith and Marios Guide to Fast Websites
keithpitt
411
23k
Typedesign – Prime Four
hannesfritz
42
2.8k
The Power of CSS Pseudo Elements
geoffreycrofte
79
6k
VelocityConf: Rendering Performance Case Studies
addyosmani
332
24k
Site-Speed That Sticks
csswizardry
13
920
GraphQLの誤解/rethinking-graphql
sonatard
73
11k
A Tale of Four Properties
chriscoyier
161
23k
Bash Introduction
62gerente
615
210k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
667
120k
The Cost Of JavaScript in 2023
addyosmani
55
9.1k
The Pragmatic Product Professional
lauravandoore
36
7k
Transcript
Kotlin ΥʔΫεϧʔ 2014-07-04 ୈ2ճ͔Θ͍͍Kotlinษڧձ ! ຊKotlinϢʔβάϧʔϓ ᖒ ଠ
Kotlinͱ • ϓϩάϥϛϯάݴޠ • JVMͰಈ͘ • JetBrains͕த৺ʹ։ൃ͍ͯ͠ΔOSS • 2011ʹൃද •
ݱόʔδϣϯ0.7
ͳͥ,PUMJOͳͷ͔ • δϟόͪΐͬͱ… • Scalaͬͯػೳ͕ଟ͍ͯ͘͠ • GroovyಈతͰ͍͠ • ͪΐ͏Ͳ͍͍ͱ͜Ζ͕ཉ͍͠ʂ
Kotlin͕ࢦ͢ͱ͜Ζ • γϯϓϧ • ҆શ • εϐʔυ ͬ͘͟Γݴ͏ͱ…
Kotlinͷಛ • ؆ܿ • ҆શ • JVMͰಈ͘ • JavaScriptʹมͰ͖Δ •
੩తܕ͚ • ΦϒδΣΫτࢦ • ؔܕϓϩάϥϛϯά
Hello World package hello fun main(args : Array<String>) { println("Hello,
world!") }
Hello World package hello fun main(args : Array<String>) { println("Hello,
world!") } ύοέʔδԼʹ ͕ؔஔ͚Δ ΤϯτϦϙΠϯτ mainؔ
Hello World package hello fun main(args : Array<String>) { println("Hello,
world!") } มͷܕ͕ޙஔ ྻ͕ δΣωϦΫε
Hello World package hello fun main(args : Array<String>) { println("Hello,
world!") } ηϛίϩϯ ෆཁ
Ϋϥε class Greeter ! val greeter = Greeter()
Ϋϥε class Greeter ! val greeter = Greeter() ࠷খͷ Ϋϥεఆٛ
Ϋϥε class Greeter ! val greeter = Greeter() ΠϯελϯεԽ newෆཁ
ܕࢦఆෆཁ: ܕਪ
Ϋϥε class Greeter(val name: String) { fun greet() { println("Hello,
$name") } }
Ϋϥε class Greeter(val name: String) { fun greet() { println("Hello,
$name") } } ϓϩύςΟ val g = Greeter(“hoge”) val name = g.name
Ϋϥε class Greeter(val name: String) { fun greet() { println("Hello,
$name") } } ϝιου ࣜ(ม)ΛຒΊࠐΊΔ
ϓϩύςΟ class User(name: String) { var name: String = name
} ! val user = User("Taro") user.name / / => "Taro"
ϓϩύςΟ class User(name: String) { var name: String = name
} ! val user = User("Taro") user.name / / => "Taro" ίϯετϥΫλ ϓϩύςΟ
ϓϩύςΟ class User(name: String) { var name: String = name
} ! val user = User("Taro") user.name / / => "Taro" ϑΟʔϧυʹ ΞΫηεͯ͠Δ෩
ΞΫηα class User(name: String) { var name: String = name
set(name: String) {$name = name } get(): String = $name }
ΞΫηα class User(name: String) { var name: String = name
set(name: String) {$name = name } get(): String = $name } Setter Getter Backing Field
Delegated Property class User(name: String) { val name: String by
Decorator(name) } ! class Decorator(val str: String) { fun get(thisRef: Any, prop:PropertyMetadata): String = "<<$str>>" }
Delegated Property class User(name: String) { val name: String by
Decorator(name) } ! class Decorator(val str: String) { fun get(thisRef: Any, prop:PropertyMetadata): String = "<<$str>>" } User#nameͷ ΞΫηε͕ҕৡ͞ΕΔ
Ԡ༻ྫ class MainActivity: Activity() { val submitButton: Button by injector()
! fun onCreate(savedInstanceState: Bundle?) { submitButton.setOnClickListener(::submit) } }
ܕ҆શ val a: Array<Int> = array(1, 2, 3) val b:
Array<Number> = a / / NG val c: Array<out Number> = a / / OK ! a[0] = 5 / / OK c[0] = 5 / / NG
ܕ҆શ val a: Array<Int> = array(1, 2, 3) val b:
Array<Number> = a / / NG val c: Array<out Number> = a / / OK ! a[0] = 5 / / OK c[0] = 5 / / NG ArrayσϑΥϧτͰ ෆม(invariant) มҐࢦఆ͕Մೳ ͜͜Ͱڞม(covariant)
ܕ҆શ val a: Array<Int> = array(1, 2, 3) val b:
Array<Number> = a / / Error val c: Array<out Number> = a / / OK ! a[0] = 5 / / OK c[0] = 5 / / NG ܕͷෆ߹Λ ͍Ͱ͘ΕΔ
એݴՕॴࢄ val a: List<Int> = listOf(1, 2, 3) val b:
List<Number> = a / / OK ———————————————————————— class List<out T> …
એݴՕॴࢄ val a: List<Int> = listOf(1, 2, 3) val b:
List<Number> = a ———————————————————————— class List<out T> … Ϋϥεͷએݴ࣌ʹ มҐࢦఆͰ͖Δ
͕ؔfirst-class val add = { (a: Int, b: Int) ->
a + b } ! add(3, 4) / / => 7
͕ؔfirst-class list.filter({ (e: Int) -> e % 2 != 0
})
͕ؔfirst-class list filter { it % 2 != 0 }
Single Abstract Method / / Java static void invoke(Runnable r)
{ r.run(); } ! / / Kotlin Sample.invoke { hoge() } RunnableͷΘΓʹ ()->UnitͳؔΛࢦఆ
ΠϯϥΠϯؔ inline fun invoke(f: ()->Unit) { f() }
ΠϯϥΠϯؔ inline fun invoke(f: ()->Unit) { f() } inlineΞϊςʔγϣϯ: ΠϯϥΠϯల։͞ΕΔ
ؔͷܕ (Ҿͷܕ)->ฦͷܕ
֦ுؔ fun String.hello() { println("Hello, $this") } ! "World".hello()
֦ுؔ + ؔΦϒδΣΫτ fun String.hello() { println("Hello, $this") } !
"World".hello()
TCO tailRecursive fun sum(ints: List<Int>, sum: Int = 0): Int
= if(ints.isEmpty()) sum else sum(ints.tail, sum + ints.first())
TCO tailRecursive fun sum(ints: List<Int>, sum: Int = 0): Int
= if(ints.isEmpty()) sum else sum(ints.tail, sum + ints.first()) tailRecursive Ξϊςʔγϣϯ
null҆શ val a: Int = null / / OK val
b: Int? = null / / NG b.toString() / / NG if(b != null) b.toString() / / OK b?.toString() / / OK
null҆શ val a: Int = null / / OK val
b: Int? = null / / NG b.toString() / / NG if(b != null) b.toString() / / OK b?.toString() / / OK if (b != null) b.toString() else null
null҆શ fun toInt(s: String): Int? fun square(n: Int): Int จࣈྻΛtoInt()Ͱʹม
ͦͷΛsquare()Ͱೋ͍ͨ͠
ෳࡶ… if(s != null) { val i = toInt(s) if(i
!= null) square(i) else null } else { null }
ศརͳؔΛಋೖ fun <T, R> T.bind(f: ((T) -> R)?): R =
f?.invoke(this)
bindΛ͏ͱΩϨΠʹͳΔ s?.bind(::toInt)?.bind(::square)
͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠