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 is here
Search
Iñaki Villar
July 09, 2017
Technology
0
93
Kotlin is here
Intro to Kotlin.
Google IO Extended Cebu
Iñaki Villar
July 09, 2017
Tweet
Share
More Decks by Iñaki Villar
See All by Iñaki Villar
Scaling Android Builds in Pandemic Times
cdsap
1
130
Building Android Projects with kts
cdsap
2
270
The Build Shrugged
cdsap
1
68
State of Testing in Kotlin
cdsap
0
190
Dexs, R8 and 3.3
cdsap
0
320
Deep Dive Work Manager
cdsap
0
260
Advanced Topics Android
cdsap
0
96
Kotlin: Server-Client
cdsap
0
81
Droidcon Dubai : Kotlin - Server - Client
cdsap
0
48
Other Decks in Technology
See All in Technology
継続的にアウトカムを生み出し ビジネスにつなげる、 戦略と運営に対するタイミーのQUEST(探求)
zigorou
0
590
【re:Invent 2024 アプデ】 Prompt Routing の紹介
champ
0
150
コンテナセキュリティのためのLandlock入門
nullpo_head
2
320
統計データで2024年の クラウド・インフラ動向を眺める
ysknsid25
2
850
WACATE2024冬セッション資料(ユーザビリティ)
scarletplover
0
210
DevOps視点でAWS re:invent2024の新サービス・アプデを振り返ってみた
oshanqq
0
180
サーバレスアプリ開発者向けアップデートをキャッチアップしてきた #AWSreInvent #regrowth_fuk
drumnistnakano
0
200
1等無人航空機操縦士一発試験 合格までの道のり ドローンミートアップ@大阪 2024/12/18
excdinc
0
170
podman_update_2024-12
orimanabu
1
280
株式会社ログラス − エンジニア向け会社説明資料 / Loglass Comapany Deck for Engineer
loglass2019
3
32k
[Ruby] Develop a Morse Code Learning Gem & Beep from Strings
oguressive
1
170
組織に自動テストを書く文化を根付かせる戦略(2024冬版) / Building Automated Test Culture 2024 Winter Edition
twada
PRO
17
4.7k
Featured
See All Featured
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
127
18k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
17
2.3k
Code Reviewing Like a Champion
maltzj
520
39k
Intergalactic Javascript Robots from Outer Space
tanoku
270
27k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
28
900
Fashionably flexible responsive web design (full day workshop)
malarkey
405
66k
BBQ
matthewcrist
85
9.4k
Building Your Own Lightsaber
phodgson
103
6.1k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
111
49k
Why You Should Never Use an ORM
jnunemaker
PRO
54
9.1k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
10
810
Bootstrapping a Software Product
garrettdimon
PRO
305
110k
Transcript
Kotlin is here Too much fun
None
None
None
None
None
.java java compiler .class .jar dex
.kt .java java compiler .class .jar dex kotlin compiler
class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) }
class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) }
class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) }
class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) }
override fun onResume() { super.onResume() mAutofillManager?.registerCallback(mAutofillCallback) } override fun onPause()
{ super.onPause() mAutofillManager?.unregisterCallback(mAutofillCallback) }
None
var index = structure.addChildCount(chidrenSize) val context : Context = request.getContext()
Variables
var index = structure.addChildCount(chidrenSize) val context : Context = request.getContext()
Variables
var index = structure.addChildCount(chidrenSize) val context : Context = request.getContext()
Variables
var index = structure.addChildCount(chidrenSize) val context : Context = request.getContext()
Variables var index : Int = structure.addChildCount(chidrenSize)
var textValue : String = "" textValue = "alo" var
index = structure.addChildCount(chidrenSize) val context : Context = request.getContext() Variables
var textValue : String = "" textValue = "alo" var
index = structure.addChildCount(chidrenSize) val context : Context = request.getContext() Variables val textValue : String = "" textValue = "alo"
var textValue : String = "" textValue = "alo" var
index = structure.addChildCount(chidrenSize) val context : Context = request.getContext() Variables val textValue : String = "" textValue = "alo"
class SimpleClass Classes/Objects
class SimpleClass open class SimpleClass Classes/Objects
class SimpleClass open class SimpleClass class SimpleClass : BaseClass() Classes/Objects
class SimpleClass open class SimpleClass class SimpleClass : BaseClass() class
SimpleClass : SimpleInterface Classes/Objects
class SimpleClass open class SimpleClass class SimpleClass : BaseClass() class
SimpleClass : SimpleInterface object SimpleClass Classes/Objects
class SimpleClass(value: String) Classes/Objects
class SimpleClass(value: String) Classes/Objects class Team { val player :
String var goals : Int }
class SimpleClass(value: String) Classes/Objects class Team { val player :
String var goals : Int }
class SimpleClass(value: String) Classes/Objects class Team { val player :
String = “Rivaldo” var goals : Int = 0 }
class SimpleClass(value: String) Classes/Objects val myPlayer = Team(“Rivaldo”,0) class Team(player:
String, goals: Int)
class SimpleClass(value: String) Classes/Objects val myPlayer = Team(“Rivaldo”,0) class Team(player:
String) { var goals: Int = 0 } class Team(player: String, goals: Int)
class SimpleClass(value: String) Classes/Objects val myPlayer = Team(“Rivaldo”,0) class Team(player:
String) { var goals: Int = 0 } val myPlayer = Team("Rivaldo") myPlayer.goals = 3 class Team(player: String, goals: Int)
Classes/Objects var goals: Int = 0 set(value) { if (goals
> 5) { log(“$player has scored multiple goals") field = value } }
Classes/Objects var goals: Int = 0 set(value) { if (goals
> 5) { log(“$player has scored multiple goals") field = value } } val isEmpty get() = this.size == 0
class SimpleClass(value: String){ init { log() } } Classes/Objects
class SimpleClass(value: String){ init { log() } } Classes/Objects class
Button : View { constructor(ctx: Context) : super(ctx) constructor(ctx: Context, attr: AttributeSet) : super(ctx, attr) }
Classes/Objects data class Team (val name : String, val goals
: Int)
Classes/Objects data class Team (val name : String, val goals
: Int) hashCode - toString - equals
Classes/Objects class A { companion object { fun bar() {
println("Companion Object") } } } data class Team (val name : String, val goals : Int) hashCode - toString - equals
Classes/Objects class A { companion object { fun bar() {
println("Companion Object") } } } data class Team (val name : String, val goals : Int) hashCode - toString - equals A.bar()
Smart Casts public void printTypeProperty(Object object) { if (object instanceof
String) { System.out.print(((String) object).isEmpty()); } else if (object instanceof Integer){ System.out.print(((Integer) object).intValue()); } else if (object instanceof Boolean){ System.out.print(((Boolean) object).booleanValue()); } }
fun printTypeProperty(any: Any) { if (any is String) { println(any.length)
} else if (any is Int) { println(any.plus(1)) } else if (any is Boolean) { println(any.not()) } } Smart Casts public void printTypeProperty(Object object) { if (object instanceof String) { System.out.print(((String) object).isEmpty()); } else if (object instanceof Integer){ System.out.print(((Integer) object).intValue()); } else if (object instanceof Boolean){ System.out.print(((Boolean) object).booleanValue()); } }
fun printTypeProperty(any: Any) { if (any is String) { println(any.length)
} else if (any is Int) { println(any.plus(1)) } else if (any is Boolean) { println(any.not()) } } Smart Casts public void printTypeProperty(Object object) { if (object instanceof String) { System.out.print(((String) object).isEmpty()); } else if (object instanceof Integer){ System.out.print(((Integer) object).intValue()); } else if (object instanceof Boolean){ System.out.print(((Boolean) object).booleanValue()); } }
sealed class Expr data class Const(val number: Double) : Expr()
data class Sum(val e1: Expr, val e2: Expr) : Expr() object NotANumber : Expr() Sealed Classes
sealed class Expr data class Const(val number: Double) : Expr()
data class Sum(val e1: Expr, val e2: Expr) : Expr() object NotANumber : Expr() Sealed Classes
sealed class Expr data class Const(val number: Double) : Expr()
data class Sum(val e1: Expr, val e2: Expr) : Expr() object NotANumber : Expr() Sealed Classes
sealed class Expr data class Const(val number: Double) : Expr()
data class Sum(val e1: Expr, val e2: Expr) : Expr() object NotANumber : Expr() fun eval(expr: Expr): Double = when(expr) { is Const -> expr.number is Sum -> eval(expr.e1) + eval(expr.e2) NotANumber -> Double.NaN } Sealed Classes
sealed class Expr data class Const(val number: Double) : Expr()
data class Sum(val e1: Expr, val e2: Expr) : Expr() object NotANumber : Expr() fun eval(expr: Expr): Double = when(expr) { is Const -> expr.number is Sum -> eval(expr.e1) + eval(expr.e2) NotANumber -> Double.NaN } Sealed Classes
sealed class Expr data class Const(val number: Double) : Expr()
data class Sum(val e1: Expr, val e2: Expr) : Expr() object NotANumber : Expr() fun eval(expr: Expr): Double = when(expr) { is Const -> expr.number is Sum -> eval(expr.e1) + eval(expr.e2) NotANumber -> Double.NaN } Sealed Classes
Interfaces class SimpleClass : SimpleInterface
interface SimpleInterface { fun doSomething() } Interfaces class SimpleClass :
SimpleInterface
interface SimpleInterface { fun doSomething() } interface SimpleInterface { fun
doSomething() fun doAnotherThing() = println("default") } Interfaces class SimpleClass : SimpleInterface
int strLen(String s){ return s.length(); } Nullability
fun strLen(s: String) = s.length int strLen(String s){ return s.length();
} Nullability
fun strLen(s: String) = s.length int strLen(String s){ return s.length();
} fun strLen(s: String?) = … Nullability
fun strLen(s: String) = s.length int strLen(String s){ return s.length();
} fun strLen(s: String?) = … fun strLen(s: String?): Int = if (s != null) s.length else 0 Nullability
fun toUpperCase(s: String?): String? Nullability
fun toUpperCase(s: String?): String? fun toUpperCase(s: String?): String? = s?.toUpperCase()
Nullability
fun toUpperCase(s: String?): String? fun toUpperCase(s: String?): String? = s?.toUpperCase()
Nullability
fun toUpperCase(s: String?): String? fun toUpperCase(s: String?): String? = s?.toUpperCase()
fun length(s: String?): Int Nullability
fun toUpperCase(s: String?): String? fun toUpperCase(s: String?): String? = s?.toUpperCase()
fun length(s: String?): Int fun length(s: String?): Int = s?.length ?: 0 Nullability
fun toUpperCase(s: String?): String? fun toUpperCase(s: String?): String? = s?.toUpperCase()
fun length(s: String?): Int fun length(s: String?): Int = s?.length ?: 0 Nullability
fun toUpperCase(s: String?): String? fun toUpperCase(s: String?): String? = s?.toUpperCase()
fun length(s: String?): Int fun length(s: String?): Int = s?.length ?: 0 Nullability
this as? PageObjectKt Nullability
this as? PageObjectKt Nullability email?.let { email -> sendEmailTo()}
fun max(a: Int, b: Int): Int { return if (a
> b) a else b } Functions
fun max(a: Int, b: Int): Int { return if (a
> b) a else b } Functions
fun max(a: Int, b: Int): Int { return if (a
> b) a else b } Functions
fun max(a: Int, b: Int): Int { return if (a
> b) a else b } Functions
fun createPlayer(name: String, lastName: String, nickName: String?, city: String?) {
Functions - Named Arguments
fun createPlayer(name: String, lastName: String, nickName: String?, city: String?) {
createPlayer("Inaki", "Villar", null, null) Functions - Named Arguments
fun createPlayer(name: String, lastName: String, nickName: String?, city: String?) {
createPlayer("Inaki", "Villar", null, null) createPlayer("Inaki", "Villar", nickName = null, city = null) Functions - Named Arguments
fun createPlayer(name: String, lastName: String, nickName: String = “", city:
String = "Cebu") Functions - Default Arguments
fun createPlayer(name: String, lastName: String, nickName: String = “", city:
String = "Cebu") createPlayer("Inaki", "Villar") Functions - Default Arguments createPlayer("Inaki", "Villar", "Nine")
fun lengthIsGreaterThanFive(s : String) = s.length > 5 val va
= "myVeryLongString" val isGreaterThanFive = lengthIsGreaterThanFive(va) Extension Functions
fun lengthIsGreaterThanFive(s : String) = s.length > 5 val va
= "myVeryLongString" val isGreaterThanFive = lengthIsGreaterThanFive(va) Extension Functions fun String.lengthIsGreaterThanFive() = this.length > 5
fun lengthIsGreaterThanFive(s : String) = s.length > 5 val va
= "myVeryLongString" val isGreaterThanFive = lengthIsGreaterThanFive(va) Extension Functions fun String.lengthIsGreaterThanFive() = this.length > 5
fun lengthIsGreaterThanFive(s : String) = s.length > 5 val va
= "myVeryLongString" val isGreaterThanFive = lengthIsGreaterThanFive(va) val va = "myVeryLongString" val isGreaterThanFive = va.lengthIsGreaterThanFive() Extension Functions fun String.lengthIsGreaterThanFive() = this.length > 5
Infix Functions infix fun Int.minus(value: Int) = this - value
Infix Functions infix fun Int.minus(value: Int) = this - value
Infix Functions infix fun Int.minus(value: Int) = this - value
val result = 4 minus 2
Infix Functions infix fun Int.minus(value: Int) = this - value
val result = 4 minus 2
Infix Functions infix fun Int.minus(value: Int) = this - value
val result = 4 minus 2 4.minus(2)
Infix Functions infix fun Int.minus(value: Int) = this - value
val result = 4 minus 2 infix fun String.repeat(times: Int) = { var result = "" for (i in 1..times) { result += this } }
Infix Functions infix fun Int.minus(value: Int) = this - value
val result = 4 minus 2 infix fun String.repeat(times: Int) = { var result = "" for (i in 1..times) { result += this } }
Infix Functions infix fun Int.minus(value: Int) = this - value
val result = 4 minus 2 infix fun String.repeat(times: Int) = { var result = "" for (i in 1..times) { result += this } } val multipleA = "a" repeat 10
Lambdas
buttonSend.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v){ send(); } });
Lambdas
buttonSend.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v){ send(); } });
buttonSend.setOnClickListener(object :OnClickListener{ override fun onClick(view : View){ send(); } }) Lambdas
buttonSend.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v){ send(); } });
buttonSend.setOnClickListener(object :OnClickListener{ override fun onClick(view : View){ send(); } }) buttonSend.setOnClickListener(View.OnClickListener { view : View -> doSomething() }) Lambdas
{ x : Int, y: Int -> x + y
} Lambdas
{ x : Int, y: Int -> x + y
} Lambdas
{ x : Int, y: Int -> x + y
} Lambdas
data class Team (val name : String, val goals :
Int) val myLeague = listOf(Team("Sakuvic", 12), Team("Rivaldo",10)) Lambdas
data class Team (val name : String, val goals :
Int) val myLeague = listOf(Team("Sakuvic", 12), Team("Rivaldo",10)) myLeague.maxBy({ p: Team -> p.goals }) Lambdas
data class Team (val name : String, val goals :
Int) val myLeague = listOf(Team("Sakuvic", 12), Team("Rivaldo",10)) myLeague.maxBy({ p: Team -> p.goals }) myLeague.maxBy { p: Team -> p.goals } Lambdas
myLeague.maxBy { it.goals } data class Team (val name :
String, val goals : Int) val myLeague = listOf(Team("Sakuvic", 12), Team("Rivaldo",10)) myLeague.maxBy({ p: Team -> p.goals }) myLeague.maxBy { p: Team -> p.goals } Lambdas
None
private val mOnNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item -> when (item.itemId)
{ R.id.navigation_home -> { message.setText(R.string.title_home) return@OnNavigationItemSelectedListener true } R.id.navigation_dashboard -> { message.setText(R.string.title_dashboard) return@OnNavigationItemSelectedListener true } R.id.navigation_notifications -> { message.setText(R.string.title_notifications) return@OnNavigationItemSelectedListener true } } false Future is here
Much more Type System Delegation High Order Functions & FP
Operator Generics DSL
None
[email protected]
@inyaki_mwc Thanks