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.7k
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
370
Kotlin 最新動向2022 #tfcon #techfeed
ntaro
1
2.1k
#Ubie 狂気の認知施策と選考設計
ntaro
13
12k
UbieにおけるサーバサイドKotlin活用事例
ntaro
1
1.1k
KotlinでSpring 完全理解ガイド #jsug
ntaro
6
3.3k
Kotlinでサーバサイドを始めよう!
ntaro
1
920
Androidからサーバーサイドまで!プログラミング言語 Kotlinの魅力 #devboost
ntaro
5
2.6k
Kotlin Contracts #m3kt
ntaro
4
3.8k
How_to_Test_Server-side_Kotlin.pdf
ntaro
1
430
Other Decks in Programming
See All in Programming
3 Effective Rules for Using Signals in Angular
manfredsteyer
PRO
0
120
AWS Lambdaから始まった Serverlessの「熱」とキャリアパス / It started with AWS Lambda Serverless “fever” and career path
seike460
PRO
1
260
WebフロントエンドにおけるGraphQL(あるいはバックエンドのAPI)との向き合い方 / #241106_plk_frontend
izumin5210
4
1.4k
Tauriでネイティブアプリを作りたい
tsucchinoko
0
370
ECS Service Connectのこれまでのアップデートと今後のRoadmapを見てみる
tkikuc
2
260
よくできたテンプレート言語として TypeScript + JSX を利用する試み / Using TypeScript + JSX outside of Web Frontend #TSKaigiKansai
izumin5210
6
1.8k
NSOutlineView何もわからん:( 前編 / I Don't Understand About NSOutlineView :( Pt. 1
usagimaru
0
340
Laravel や Symfony で手っ取り早く OpenAPI のドキュメントを作成する
azuki
2
120
イマのCSSでできる インタラクション最前線 + CSS最新情報
clockmaker
3
350
色々なIaCツールを実際に触って比較してみる
iriikeita
0
330
Contemporary Test Cases
maaretp
0
140
PHP でアセンブリ言語のように書く技術
memory1994
PRO
1
170
Featured
See All Featured
Stop Working from a Prison Cell
hatefulcrawdad
267
20k
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
Designing on Purpose - Digital PM Summit 2013
jponch
115
7k
Code Reviewing Like a Champion
maltzj
520
39k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
16
2.1k
Fireside Chat
paigeccino
34
3k
Measuring & Analyzing Core Web Vitals
bluesmoon
4
130
Writing Fast Ruby
sferik
627
61k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
26
2.1k
Practical Orchestrator
shlominoach
186
10k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
28
2k
Automating Front-end Workflow
addyosmani
1366
200k
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)
͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠