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
80
Droidcon Dubai : Kotlin - Server - Client
cdsap
0
48
Other Decks in Technology
See All in Technology
AWS Lambda のトラブルシュートをしていて思うこと
kazzpapa3
2
180
AGIについてChatGPTに聞いてみた
blueb
0
130
Lexical Analysis
shigashiyama
1
150
New Relicを活用したSREの最初のステップ / NRUG OKINAWA VOL.3
isaoshimizu
3
630
マルチプロダクトな開発組織で 「開発生産性」に向き合うために試みたこと / Improving Multi-Product Dev Productivity
sugamasao
1
310
エンジニア人生の拡張性を高める 「探索型キャリア設計」の提案
tenshoku_draft
1
130
適材適所の技術選定 〜GraphQL・REST API・tRPC〜 / Optimal Technology Selection
kakehashi
1
690
飲食店データの分析事例とそれを支えるデータ基盤
kimujun
0
160
初心者向けAWS Securityの勉強会mini Security-JAWSを9ヶ月ぐらい実施してきての近況
cmusudakeisuke
0
130
Evangelismo técnico: ¿qué, cómo y por qué?
trishagee
0
360
SRE×AIOpsを始めよう!GuardDutyによるお手軽脅威検出
amixedcolor
0
170
Oracle Cloud Infrastructureデータベース・クラウド:各バージョンのサポート期間
oracle4engineer
PRO
28
13k
Featured
See All Featured
Raft: Consensus for Rubyists
vanstee
136
6.6k
Imperfection Machines: The Place of Print at Facebook
scottboms
265
13k
Six Lessons from altMBA
skipperchong
27
3.5k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
232
17k
The Pragmatic Product Professional
lauravandoore
31
6.3k
Code Review Best Practice
trishagee
64
17k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.3k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
109
49k
Building Your Own Lightsaber
phodgson
103
6.1k
Scaling GitHub
holman
458
140k
RailsConf 2023
tenderlove
29
900
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
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