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
180
Dexs, R8 and 3.3
cdsap
0
310
Deep Dive Work Manager
cdsap
0
230
Advanced Topics Android
cdsap
0
95
Kotlin: Server-Client
cdsap
0
79
Droidcon Dubai : Kotlin - Server - Client
cdsap
0
46
Other Decks in Technology
See All in Technology
一休.comレストランにおけるRustの活用
kymmt90
3
570
アジャイルと契約 エッセンシャル版 / Agile Contracts Essential Edition
fkino
0
110
「最高のチューニング」をしないために / hack@delta 24.10
fujiwara3
20
3.3k
カメラを用いた店内計測におけるオプトインの仕組みの実現 / ai-optin-camera
cyberagentdevelopers
PRO
1
120
[AWS JAPAN 生成AIハッカソン] Dialog の紹介
yoshimi0227
0
140
日経電子版におけるリアルタイムレコメンドシステム開発の事例紹介/nikkei-realtime-recommender-system
yng87
1
460
マネジメント視点でのre:Invent参加 ~もしCEOがre:Inventに行ったら~
kojiasai
0
410
Automated Promptingを目指すその前に / Before we can aim for Automated Prompting
rkaga
0
100
物価高なラスベガスでの過ごし方
zakky
0
320
急成長中のWINTICKETにおける品質と開発スピードと向き合ったQA戦略と今後の展望 / winticket-autify
cyberagentdevelopers
PRO
1
160
Amazon_CloudWatch_ログ異常検出_導入ガイド
tsujiba
4
1.4k
現地でMeet Upをやる場合の注意点〜反省点を添えて〜
shotashiratori
0
480
Featured
See All Featured
Optimising Largest Contentful Paint
csswizardry
33
2.9k
Agile that works and the tools we love
rasmusluckow
327
21k
Writing Fast Ruby
sferik
626
61k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
37
1.8k
Testing 201, or: Great Expectations
jmmastey
38
7k
What's in a price? How to price your products and services
michaelherold
243
12k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
92
16k
4 Signs Your Business is Dying
shpigford
180
21k
Documentation Writing (for coders)
carmenintech
65
4.4k
How GitHub (no longer) Works
holman
311
140k
10 Git Anti Patterns You Should be Aware of
lemiorhan
654
59k
RailsConf 2023
tenderlove
29
880
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