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.5k
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
260
Kotlin 最新動向2022 #tfcon #techfeed
ntaro
1
2.1k
#Ubie 狂気の認知施策と選考設計
ntaro
13
12k
UbieにおけるサーバサイドKotlin活用事例
ntaro
1
1k
KotlinでSpring 完全理解ガイド #jsug
ntaro
6
3.3k
Kotlinでサーバサイドを始めよう!
ntaro
1
900
Kotlin Contracts #m3kt
ntaro
4
3.7k
How_to_Test_Server-side_Kotlin.pdf
ntaro
1
420
Kotlin Fest 2018 - Opening session
ntaro
0
4.2k
Other Decks in Programming
See All in Programming
仮想ファイルシステムを導入して開発環境のストレージ課題を解消する
segadevtech
2
540
Crafting Cross-Platform Adventures: Building a Game Engine with Kotlin Multiplatform
dwursteisen
0
100
Amazon Neptuneで始める初めてのグラフDB ー グラフDBを使う意味を考える ー
satoshi256kbyte
2
260
What you can do with Ruby on WebAssembly
kateinoigakukun
0
160
実践!難読化ガイド
mitchan
0
160
REXML改善のその後
naitoh
0
190
Scala におけるコンパイラエラーとの付き合い方
chencmd
2
420
Go Code Generation at newmo / 2024-08-27 #newmo_layerx_go
genkey6
0
560
Google Sign-inの移行から始めるCredential Manager活用
clockvoid
0
220
Debugging: All you need to know (for simultaneous interpreting)
jmatsu
2
760
僕が思い描くTypeScriptの未来を勝手に先取りする
yukukotani
9
2.4k
LangChainの現在とv0.3にむけて
os1ma
4
910
Featured
See All Featured
How STYLIGHT went responsive
nonsquared
93
5.1k
Gamification - CAS2011
davidbonilla
79
5k
Reflections from 52 weeks, 52 projects
jeffersonlam
346
20k
A Modern Web Designer's Workflow
chriscoyier
691
190k
GitHub's CSS Performance
jonrohan
1029
450k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
5
480
Building Applications with DynamoDB
mza
90
6k
The World Runs on Bad Software
bkeepers
PRO
64
11k
Side Projects
sachag
451
42k
Making the Leap to Tech Lead
cromwellryan
128
8.8k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
131
32k
Ruby is Unlike a Banana
tanoku
96
11k
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アプリケーション用フレームワークも充実