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

Kotlin A nova linguagem oficial do Android

Kotlin A nova linguagem oficial do Android

Vamos falar sobre Kotlin, que recentemente foi divulgada no Google IO 2017 como a linguagem oficial para Android.
O objetivo será mostrar algumas características da linguagem e como aplica-las no Android.

B59b1bd643ff429bb27032b05d4f2e23?s=128

Houssan A. Hijazi

June 05, 2017
Tweet

Transcript

  1. Kotlin 
 A nova linguagem oficial do Android

  2. GDGFoz Quem ? • Houssan Ali Hijazi - hussanhijazi@gmail.com •

    Desenvolvedor Android na www.HElabs.com • Organizador GDG Foz do Iguaçu • www.lojasnoparaguai.com.br • www.desaparecidosbr.org • www.hussan.com.br
  3. GDGFoz Kotlin • 2011/JetBrains • 1.0 em Fev. 2016 •

    1.1 em Mar. 2017 • Pinterest, Coursera, Netflix, Uber, Square, Trello e Basecamp • Google IO 2017
  4. GDGFoz Kotlin

  5. GDGFoz Kotlin • Interoperabilidade • Null Safety • Conciso

  6. GDGFoz Properties/Fields val name: String = "João" val name =

    "João"
  7. GDGFoz val/var val user = User() user = User() //

    :( var user = User() user = User() // :)
  8. GDGFoz Null Safety/Safe Calls var name: String? = "José"
 println(name?.length)

    var name: String = "José" name = null // :( var name: String? = "José"
 name = null // :)
 val lastName: String? = null
  9. GDGFoz Elvis Operator val name: String? = "José" val c:

    Int = if (name != null) name.length else -1 val c = name?.length ?: -1
  10. GDGFoz Class class Car(val name: String = ""): Vehicle() {

    constructor(name: String, color: String) : this(name) { } }
  11. GDGFoz Kotlin class Car { 
 private String name; public

    String getName() { //... } public void setName(String s) { //... } } // ^^Java // vv Kotlin val car = Car() println("Name: ${car.name}")
  12. GDGFoz Functions fun calculate(num: Int, num2: Int = 10): Int

    { return num + num2 } fun calculate(num: Int, num2: Int): Int = num + num2
 fun calculate(num: Int, num2: Int) = num + num2 calculate(num = 10, num2 = 20)
  13. GDGFoz Interface interface BaseView { fun bar() fun foo() {

    // optional body } } class View : BaseView { override fun bar() { } }
  14. GDGFoz Data Class data class Truck(val name:String, val color: String

    = "Red")
  15. GDGFoz For/Range // prints number from 1 through 10 for

    (i in 1..10) { print(i) } // prints 100, 98, 96 ... for (i in 100 downTo 1 step 2) { print(i) }
 
 for (item in collection) print(item)
  16. GDGFoz When when (x) { in 1..10 -> print("x is

    in the range") !in 10..20 -> print("x is outside the range") else -> print("none of the above") } fun hasPrefix(x: Any) = when(x) { is String -> x.startsWith("prefix") else -> false } when (x) { 1 -> print("x == 1") 2 -> print("x == 2") else -> { print("x is neither 1 nor 2") } }

  17. GDGFoz Collections val set = hashSetOf(1, 2, 3) val list

    = arrayListOf(1, 2, 3) val items = listOf(1, 2, 3, 4) items.first() == 1 items.last() == 4 items.filter { it % 2 == 0 } // returns [2, 4] items.forEach { println(it) } // count, forEachIndexed, max, min, take, slice, map, flatMap … etc.. 
 // More in https://antonioleiva.com/collection-operations- kotlin/
  18. GDGFoz Lambdas showButton.setOnClickListener(new View.OnClickListener() { public void onClick(View button) {

    text.setVisibility(View.VISIBLE) button.setBackgroundColor(ContextCompat.getColor(this, R.color.colorAccent)) } });
 // ^^ Java showButton.setOnClickListener { button -> text.visibility = View.VISIBLE button.setBackgroundColor(ContextCompat.getColor(thi s, R.color.colorAccent)) } // ^^Kotlin
  19. GDGFoz Lambdas // ^^ Class validate(this::toast) fun validate(callback: () ->

    Unit) { // validate callback.invoke() } fun toast() { val toast = Toast.makeText(this, "Callback called", Toast.LENGTH_LONG) toast.show() }
  20. GDGFoz let/with var name: String? = "João" 
 name?.let {

    println(it.toUpperCase()) println(it.length) } with(name) { println(toUpperCase()) println(length) }
 // See: apply/run
  21. GDGFoz Infix infix fun String.drive(car: String) = Pair(this, car)
 val

    usersCars = mapOf("José" drive "Fusca", "João" drive "Ferrari") 
 val usersCars2 = mapOf("José".drive("Fusca"), "João".drive("Ferrari")); val (driver, car) = "José" drive "Fusca"
  22. GDGFoz Extensions fun View.hide() { visibility = View.GONE } fun

    View.show() { visibility = View.VISIBLE } button.setVisibility(View.VISIBLE) var button: Button = findViewById(R.id.button) as Button button.hide()
  23. Obrigado