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.

Houssan A. Hijazi

June 05, 2017
Tweet

More Decks by Houssan A. Hijazi

Other Decks in Programming

Transcript

  1. GDGFoz Quem ? • Houssan Ali Hijazi - [email protected]

    Desenvolvedor Android na www.HElabs.com • Organizador GDG Foz do Iguaçu • www.lojasnoparaguai.com.br • www.desaparecidosbr.org • www.hussan.com.br
  2. 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
  3. GDGFoz val/var val user = User() user = User() //

    :( var user = User() user = User() // :)
  4. 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
  5. GDGFoz Elvis Operator val name: String? = "José" val c:

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

    constructor(name: String, color: String) : this(name) { } }
  7. 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}")
  8. 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)
  9. GDGFoz Interface interface BaseView { fun bar() fun foo() {

    // optional body } } class View : BaseView { override fun bar() { } }
  10. 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)
  11. 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") } }

  12. 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/
  13. 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
  14. 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() }
  15. 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
  16. 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"
  17. 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()