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.6k
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
520
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
950
Kotlin Contracts #m3kt
ntaro
4
3.9k
How_to_Test_Server-side_Kotlin.pdf
ntaro
1
470
Kotlin Fest 2018 - Opening session
ntaro
0
4.2k
Other Decks in Programming
See All in Programming
富山発の個人開発サービスで日本中の学校の業務を改善した話
krpk1900
4
380
2,500万ユーザーを支えるSREチームの6年間のスクラムのカイゼン
honmarkhunt
6
5.3k
Linux && Docker 研修/Linux && Docker training
forrep
24
4.5k
GoとPHPのインターフェイスの違い
shimabox
2
180
DROBEの生成AI活用事例 with AWS
ippey
0
130
CI改善もDatadogとともに
taumu
0
110
Grafana Cloudとソラカメ
devoc
0
170
Introduction to kotlinx.rpc
arawn
0
690
ARA Ansible for the teams
kksat
0
150
SwiftUIで単方向アーキテクチャを導入して得られた成果
takuyaosawa
0
270
JavaScriptツール群「UnJS」を5分で一気に駆け巡る!
k1tikurisu
9
1.8k
Conform を推す - Advocating for Conform
mizoguchicoji
3
690
Featured
See All Featured
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
226
22k
BBQ
matthewcrist
87
9.5k
The World Runs on Bad Software
bkeepers
PRO
67
11k
Docker and Python
trallard
44
3.3k
Build The Right Thing And Hit Your Dates
maggiecrowley
34
2.5k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
21
2.5k
A designer walks into a library…
pauljervisheath
205
24k
[RailsConf 2023] Rails as a piece of cake
palkan
53
5.2k
Bootstrapping a Software Product
garrettdimon
PRO
306
110k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
133
33k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
248
1.3M
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
30
4.6k
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アプリケーション用フレームワークも充実