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
700
Kotlin 最新動向2022 #tfcon #techfeed
ntaro
1
2.2k
#Ubie 狂気の認知施策と選考設計
ntaro
13
13k
UbieにおけるサーバサイドKotlin活用事例
ntaro
1
1.1k
KotlinでSpring 完全理解ガイド #jsug
ntaro
6
3.4k
Kotlinでサーバサイドを始めよう!
ntaro
1
990
Androidからサーバーサイドまで!プログラミング言語 Kotlinの魅力 #devboost
ntaro
5
2.8k
Kotlin Contracts #m3kt
ntaro
4
4.1k
How_to_Test_Server-side_Kotlin.pdf
ntaro
1
500
Other Decks in Programming
See All in Programming
AIともっと楽するE2Eテスト
myohei
8
3k
「App Intent」よくわからんけどすごい!
rinngo0302
1
100
What's new in AppKit on macOS 26
1024jp
0
150
初学者でも今すぐできる、Claude Codeの生産性を10倍上げるTips
s4yuba
16
13k
Quand Symfony, ApiPlatform, OpenAI et LangChain s'allient pour exploiter vos PDF : de la théorie à la production…
ahmedbhs123
0
220
猫と暮らす Google Nest Cam生活🐈 / WebRTC with Google Nest Cam
yutailang0119
0
170
「テストは愚直&&網羅的に書くほどよい」という誤解 / Test Smarter, Not Harder
munetoshi
0
200
PHP 8.4の新機能「プロパティフック」から学ぶオブジェクト指向設計とリスコフの置換原則
kentaroutakeda
2
1k
Rails Frontend Evolution: It Was a Setup All Along
skryukov
0
280
テスト駆動Kaggle
isax1015
1
620
AI コーディングエージェントの時代へ:JetBrains が描く開発の未来
masaruhr
1
200
TypeScriptでDXを上げろ! Hono編
yusukebe
3
770
Featured
See All Featured
Become a Pro
speakerdeck
PRO
29
5.4k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
282
13k
Keith and Marios Guide to Fast Websites
keithpitt
411
22k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.4k
Building Adaptive Systems
keathley
43
2.7k
Build The Right Thing And Hit Your Dates
maggiecrowley
37
2.8k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
18
990
Practical Orchestrator
shlominoach
189
11k
A Tale of Four Properties
chriscoyier
160
23k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
53
2.9k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
26
2.9k
How to Ace a Technical Interview
jacobian
278
23k
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)
͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠