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
47
Other Decks in Technology
See All in Technology
サイバーセキュリティと認知バイアス:対策の隙を埋める心理学的アプローチ
shumei_ito
0
380
TypeScriptの次なる大進化なるか!? 条件型を返り値とする関数の型推論
uhyo
2
1.6k
いざ、BSC討伐の旅
nikinusu
2
780
20241120_JAWS_東京_ランチタイムLT#17_AWS認定全冠の先へ
tsumita
2
250
OCI Vault 概要
oracle4engineer
PRO
0
9.7k
マルチプロダクトな開発組織で 「開発生産性」に向き合うために試みたこと / Improving Multi-Product Dev Productivity
sugamasao
1
300
[CV勉強会@関東 ECCV2024 読み会] オンラインマッピング x トラッキング MapTracker: Tracking with Strided Memory Fusion for Consistent Vector HD Mapping (Chen+, ECCV24)
abemii
0
220
100 名超が参加した日経グループ横断の競技型 AWS 学習イベント「Nikkei Group AWS GameDay」の紹介/mediajaws202411
nikkei_engineer_recruiting
1
170
Security-JAWS【第35回】勉強会クラウドにおけるマルウェアやコンテンツ改ざんへの対策
4su_para
0
180
【令和最新版】AWS Direct Connectと愉快なGWたちのおさらい
minorun365
PRO
5
750
dev 補講: プロダクトセキュリティ / Product security overview
wa6sn
1
2.3k
10XにおけるData Contractの導入について: Data Contract事例共有会
10xinc
6
620
Featured
See All Featured
Site-Speed That Sticks
csswizardry
0
23
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
159
15k
Designing on Purpose - Digital PM Summit 2013
jponch
115
7k
For a Future-Friendly Web
brad_frost
175
9.4k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
42
9.2k
Put a Button on it: Removing Barriers to Going Fast.
kastner
59
3.5k
Agile that works and the tools we love
rasmusluckow
327
21k
KATA
mclloyd
29
14k
What's in a price? How to price your products and services
michaelherold
243
12k
Statistics for Hackers
jakevdp
796
220k
jQuery: Nuts, Bolts and Bling
dougneiner
61
7.5k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
28
9.1k
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