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
LINEヤフーのフロントエンド組織・体制の紹介【24年12月】
lycorp_recruit_jp
0
530
株式会社ログラス − エンジニア向け会社説明資料 / Loglass Comapany Deck for Engineer
loglass2019
3
31k
Amazon Kendra GenAI Index 登場でどう変わる? 評価から学ぶ最適なRAG構成
naoki_0531
0
100
生成AIをより賢く エンジニアのための RAG入門 - Oracle AI Jam Session #20
kutsushitaneko
4
220
ハイテク休憩
sat
PRO
2
140
2024年にチャレンジしたことを振り返るぞ
mitchan
0
130
Amazon SageMaker Unified Studio(Preview)、Lakehouse と Amazon S3 Tables
ishikawa_satoru
0
150
社外コミュニティで学び社内に活かす共に学ぶプロジェクトの実践/backlogworld2024
nishiuma
0
260
Storage Browser for Amazon S3
miu_crescent
1
140
AIのコンプラは何故しんどい?
shujisado
1
190
権威ドキュメントで振り返る2024 #年忘れセキュリティ2024
hirotomotaguchi
2
740
1等無人航空機操縦士一発試験 合格までの道のり ドローンミートアップ@大阪 2024/12/18
excdinc
0
150
Featured
See All Featured
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
[RailsConf 2023] Rails as a piece of cake
palkan
53
5k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
28
900
4 Signs Your Business is Dying
shpigford
181
21k
A designer walks into a library…
pauljervisheath
204
24k
Building an army of robots
kneath
302
44k
The Illustrated Children's Guide to Kubernetes
chrisshort
48
48k
GraphQLとの向き合い方2022年版
quramy
44
13k
Code Reviewing Like a Champion
maltzj
520
39k
Build your cross-platform service in a week with App Engine
jlugia
229
18k
Raft: Consensus for Rubyists
vanstee
137
6.7k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
10
810
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