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
Androidからサーバーサイドまで!プログラミング言語 Kotlinの魅力 #devboost
Search
Taro Nagasawa
December 15, 2018
Programming
5
2.8k
Androidからサーバーサイドまで!プログラミング言語 Kotlinの魅力 #devboost
Developers Boots (
https://event.shoeisha.jp/devboost/20181215
)で発表したスライドです。
Taro Nagasawa
December 15, 2018
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
Kotlin Contracts #m3kt
ntaro
4
4.2k
How_to_Test_Server-side_Kotlin.pdf
ntaro
1
520
Kotlin Fest 2018 - Opening session
ntaro
0
4.3k
Other Decks in Programming
See All in Programming
Back to the Future: Let me tell you about the ACP protocol
terhechte
0
130
CI_CD「健康診断」のススメ。現場でのボトルネック特定から、健康診断を通じた組織的な改善手法
teamlab
PRO
0
180
タスクの特性や不確実性に応じた最適な作業スタイルの選択(ペアプロ・モブプロ・ソロプロ)と実践 / Optimal Work Style Selection: Pair, Mob, or Solo Programming.
honyanya
3
140
Conquering Massive Traffic Spikes in Ruby Applications with Pitchfork
riseshia
0
150
Reduxモダナイズ 〜コードのモダン化を通して、将来のライブラリ移行に備える〜
pvcresin
2
680
Pythonスレッドとは結局何なのか? CPython実装から見るNoGIL時代の変化
curekoshimizu
4
1.3k
ソフトウェア設計の実践的な考え方
masuda220
PRO
3
470
ИИ-Агенты в каждый дом – Алексей Порядин, PythoNN
sobolevn
0
150
Playwrightはどのようにクロスブラウザをサポートしているのか
yotahada3
7
2.3k
アメ車でサンノゼを走ってきたよ!
s_shimotori
0
140
フロントエンド開発に役立つクライアントプログラム共通のノウハウ / Universal client-side programming best practices for frontend development
nrslib
7
3.9k
XP, Testing and ninja testing ZOZ5
m_seki
2
280
Featured
See All Featured
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
252
21k
For a Future-Friendly Web
brad_frost
180
9.9k
GraphQLとの向き合い方2022年版
quramy
49
14k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
32
2.2k
4 Signs Your Business is Dying
shpigford
185
22k
The Invisible Side of Design
smashingmag
301
51k
Bash Introduction
62gerente
615
210k
Learning to Love Humans: Emotional Interface Design
aarron
274
40k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
30
2.9k
GitHub's CSS Performance
jonrohan
1032
460k
Embracing the Ebb and Flow
colly
88
4.8k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
45
2.5k
Transcript
2018-12-13 Developers Boost 長澤太郎 Androidからサーバーサイドまで! プログラミング言語 Kotlin の魅力
長澤 太郎 • @ngsw_taro • Ubie株式会社 ソフトウェアエンジニア • 日本Kotlinユーザグループ代表
「世界中の人々を適切な医療に案内する」
Kotlin
Kotlinとは • JetBrainsにより開発されているプログラミング言語 ◦ IntelliJ IDEAの強力な機能を利用可能 • 静的型付けオブジェクト指向言語 ◦ 言語の種類としてはJavaと同じ
• JVMやAndroidを主要なターゲットとして登場したが、 JavaScriptやNativeも重要なターゲットとしている • 2011年に発表され、安定版が2016年にリリースされた
1. Kotlinのここがスゴイ! 2. KotlinでAndroid開発 3. KotlinでWebアプリ開発
本日紹介する Kotlinのスゴイところ • 読み書きが容易な文法 • 演算子オーバロード • 拡張関数
読み書きが容易な文法: クラスとプロパティ class 分数(val 分子: Int, val 分母: Int) val
half = 分数(1, 2) println(half.分子) // 1と表示される println(half.分母) // 2と表示される
読み書きが容易な文法: 文字列テンプレート class 分数(val 分子: Int, val 分母: Int) {
override fun toString(): String { return "${分子}/${分母}" } } val half = 分数(1, 2) println(half) // 「1/2」と表示される
演算子オーバロード 独自クラスにも演算子を適用可能 class 分数(val 分子: Int, val 分母: Int) {
operator fun plus(that: 分数): 分数 { return 分数( 分子 * that.分母 + that.分子 * 分母, 分母 * that.分母 ) } ...略... val sum = 分数(1, 2) + 分数(1, 3) println(sum) 「5/6」と表示される
拡張関数 既存の型にメソッドを追加しているように見せる operator fun Int.plus(r: 分数): 分数 { return 分数(
this * r.分母 + r.分子, r.分母 ) } val sum = 2 + 分数(1, 5) println(sum) 「11/5」と表示される
1. Kotlinのここがスゴイ! 2. KotlinでAndroid開発 3. KotlinでWebアプリ開発
Android開発におけるKotlinのスゴイところ • Javaとの相互運用性 • @Percelize • コルーチン
Javaとの相互運用性: 例えばSAM変換 button.setOnClickListener(object: OnClickListener { override fun onClick(view: View) {
/* クリック時の処理 */ } }) button.setOnClickListener { /* クリック時の処理 */ }
@Percelize Percelable対応クラスを自動実装してくれる仕組み @Percelize class User( val id: Long, val name:
String ): Percelable • Androidでオブジェクトをシリアライズする仕組み • 仕様に沿った実装を提供する必要があり、冗長になりがち • JavaにはあるがKotlinには存在しない概念が必要 ◦ 回避策はあるがあまりスマートにならない
コルーチン 特にasync/awaitによる非同期処理 // コールバック方式 api.getFoo { foo -> api.getBar(foo) {
bar -> show(bar) } } // コルーチン GlobalScope.launch(Dispatchers.Main) { val foo = async { api.getFoo() }.await() val bar = async { api.getBar(foo) }.await() show(bar) }
1. Kotlinのここがスゴイ! 2. KotlinでAndroid開発 3. KotlinでWebアプリ開発
Webアプリ開発におけるKotlinのスゴイところ • Spring Framework 対応 • Kotlin向けWAF Ktor
Spring Bootプロジェクトの雛形作成 Webサイト または IntelliJ IDEA で 簡単にプロジェクト雛形を作 成可能
アノテーションベースのいつものSpring @SpringBootApplication class DemoApplication fun main(args: Array<String>) { runApplication<DemoApplication>(*args) }
@Service class HelloWorldService { fun helloWorld(): String = "Hello, world!" } @RestController class HelloWorldController(val helloWorldService: HelloWorldService) { @GetMapping("/hello-world") fun helloWorld(): String = helloWorldService.helloWorld() }
KotlinDSLによるDIとルーティング設定も fun main(args: Array<String>) { SpringApplicationBuilder() .sources(DemoApplication::class.java) .initializers(beans { bean
{ HelloWorldService() } bean { HelloWorldController(ref()) } bean { router { GET("/hello-world") { ref<HelloWorldController>().helloWorld() } } } }) .run(*args) } Bean 登録 routing 設定
Kotlin向けWebアプリフレームワーク Ktor • https://github.com/ktorio/ktor • JetBrainsにより開発 • 先日 1.0.0がリリースされた(現在 1.0.1)
• いわゆるマイクロ・フレームワーク • Kotlin DSLによるルーティング設定 • ノンブロッキング、コルーチン対応 fun main(args: Array<String>) { embeddedServer(Netty, 8080) { routing { get("/") { call.respondText("Hello, world!", ContentType.Text.Html) } } }.start(wait = true) }
まとめ • KotlinはJetBrainsに開発されている若い言語 • 書いて楽しい!読んで快適!実践主義な文法 • 相互運用性が高く、既存のJava資産を活かせる • コルーチンにより、簡単なコードで軽量スレッド •
Webアプリケーション用フレームワークも充実