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
780
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
CSC509 Lecture 06
javiergs
PRO
0
260
[Kaigi on Rais 2025] 全問正解率3%: RubyKaigiで出題したやりがちな危険コード5選
power3812
0
110
Go言語はstack overflowの夢を見るか?
logica0419
0
120
Web Components で実現する Hotwire とフロントエンドフレームワークの橋渡し / Bridging with Web Components
da1chi
3
2.1k
Conquering Massive Traffic Spikes in Ruby Applications with Pitchfork
riseshia
0
160
Things You Thought You Didn’t Need To Care About That Have a Big Impact On Your Job
hollycummins
0
210
Six and a half ridiculous things to do with Quarkus
hollycummins
0
160
All About Angular's New Signal Forms
manfredsteyer
PRO
0
110
複雑化したリポジトリをなんとかした話 pipenvからuvによるモノレポ構成への移行
satoshi256kbyte
1
1k
株式会社 Sun terras カンパニーデック
sunterras
0
280
Swift Concurrency - 状態監視の罠
objectiveaudio
2
510
AI Coding Meetup #3 - 導入セッション / ai-coding-meetup-3
izumin5210
0
3.3k
Featured
See All Featured
What's in a price? How to price your products and services
michaelherold
246
12k
How To Stay Up To Date on Web Technology
chriscoyier
791
250k
Six Lessons from altMBA
skipperchong
28
4k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
285
14k
BBQ
matthewcrist
89
9.8k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
9
860
Context Engineering - Making Every Token Count
addyosmani
5
220
Art, The Web, and Tiny UX
lynnandtonic
303
21k
How STYLIGHT went responsive
nonsquared
100
5.8k
Designing Experiences People Love
moore
142
24k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
52
5.6k
Embracing the Ebb and Flow
colly
88
4.8k
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)
͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠