Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Kotlin is here

Kotlin is here

Intro to Kotlin.
Google IO Extended Cebu

Iñaki Villar

July 09, 2017
Tweet

More Decks by Iñaki Villar

Other Decks in Technology

Transcript

  1. class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) {

    super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) }
  2. class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) {

    super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) }
  3. class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) {

    super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) }
  4. class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) {

    super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) }
  5. var index = structure.addChildCount(chidrenSize) val context : Context = request.getContext()

    Variables var index : Int = structure.addChildCount(chidrenSize)
  6. var textValue : String = "" textValue = "alo" var

    index = structure.addChildCount(chidrenSize) val context : Context = request.getContext() Variables
  7. var textValue : String = "" textValue = "alo" var

    index = structure.addChildCount(chidrenSize) val context : Context = request.getContext() Variables val textValue : String = "" textValue = "alo"
  8. var textValue : String = "" textValue = "alo" var

    index = structure.addChildCount(chidrenSize) val context : Context = request.getContext() Variables val textValue : String = "" textValue = "alo"
  9. class SimpleClass open class SimpleClass class SimpleClass : BaseClass() class

    SimpleClass : SimpleInterface object SimpleClass Classes/Objects
  10. 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)
  11. 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)
  12. Classes/Objects var goals: Int = 0 set(value) { if (goals

    > 5) { log(“$player has scored multiple goals") field = value } }
  13. 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
  14. class SimpleClass(value: String){ init { log() } } Classes/Objects class

    Button : View { constructor(ctx: Context) : super(ctx) constructor(ctx: Context, attr: AttributeSet) : super(ctx, attr) }
  15. Classes/Objects class A { companion object { fun bar() {

    println("Companion Object") } } } data class Team (val name : String, val goals : Int) hashCode - toString - equals
  16. 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()
  17. 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()); } }
  18. 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()); } }
  19. 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()); } }
  20. 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
  21. 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
  22. 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
  23. 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
  24. 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
  25. 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
  26. interface SimpleInterface { fun doSomething() } interface SimpleInterface { fun

    doSomething() fun doAnotherThing() = println("default") } Interfaces class SimpleClass : SimpleInterface
  27. 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
  28. 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
  29. 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
  30. 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
  31. fun createPlayer(name: String, lastName: String, nickName: String?, city: String?) {

    createPlayer("Inaki", "Villar", null, null) Functions - Named Arguments
  32. 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
  33. fun createPlayer(name: String, lastName: String, nickName: String = “", city:

    String = "Cebu") createPlayer("Inaki", "Villar") Functions - Default Arguments createPlayer("Inaki", "Villar", "Nine")
  34. fun lengthIsGreaterThanFive(s : String) = s.length > 5 val va

    = "myVeryLongString" val isGreaterThanFive = lengthIsGreaterThanFive(va) Extension Functions
  35. fun lengthIsGreaterThanFive(s : String) = s.length > 5 val va

    = "myVeryLongString" val isGreaterThanFive = lengthIsGreaterThanFive(va) Extension Functions fun String.lengthIsGreaterThanFive() = this.length > 5
  36. fun lengthIsGreaterThanFive(s : String) = s.length > 5 val va

    = "myVeryLongString" val isGreaterThanFive = lengthIsGreaterThanFive(va) Extension Functions fun String.lengthIsGreaterThanFive() = this.length > 5
  37. 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
  38. 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 } }
  39. 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 } }
  40. 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
  41. buttonSend.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v){ send(); } });

    buttonSend.setOnClickListener(object :OnClickListener{ override fun onClick(view : View){ send(); } }) Lambdas
  42. 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
  43. data class Team (val name : String, val goals :

    Int) val myLeague = listOf(Team("Sakuvic", 12), Team("Rivaldo",10)) Lambdas
  44. 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
  45. 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
  46. 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
  47. 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