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

Kotlin: Uma Nova Esperança

Kotlin: Uma Nova Esperança

Mickele Moriconi

May 05, 2015
Tweet

More Decks by Mickele Moriconi

Other Decks in Programming

Transcript

  1. package mobi.porquenao.poc.kotlin import java.util.* fun main(args: Array<String>) { if (args.size()

    == 0) return println("First argument: ${args[0]}") } Sintaxe docs
  2. val a: Int = 0 val b = 0 //

    Tipo Int é inferido Variáveis docs
  3. // Imutável val a: Int = 0 a = 1

    // Erro // Mutável var b = 0 b = 1 // OK Variáveis mutabilidade docs
  4. Null Safety docs // OK var x: String = "Hi"

    // Erro var x: String = null // OK var x: String? = null
  5. Null Safety docs var x: String? = "Hi" x.length //

    Erro if (x != null) { x.length // OK } x?.length // Igual acima x!!.length
  6. class User { val name: String = "Luke" var surname:

    String = "Skywalker" fun fullName(): String { return "$name $surname" } } Class docs
  7. class User(val name: String, var surname: String) { fun fullName():

    String { return "$name $surname" } } val user = User("Luke", "Skywalker") println(user.fullName()) Class docs
  8. Getters and Setters docs public class MainActivity : BaseActivity() {

    var somePreference: Boolean get() { return sharedPreferences.getBoolean("SomePreference", true) } set(value) { sharedPreferences.edit() .putBoolean("SomePreference", value) .apply() } }
  9. Lambdas java docs void calculate(Runnable after) { // Calcula algo

    after.run(); } calculate(new Runnable() { @Override public void run() { println("after"); } });
  10. Lambdas docs fun calculate(after: () -> Unit) { // Calcula

    algo after() } calculate({ println("after") }) calculate { println("after") }
  11. Lambdas docs fun sum(n1: Int, n2: Int, after: (Int) ->

    Unit) { // Soma algo after(n1 + n2) } sum(1, 2, { result -> println(result) }) sum(1, 2) { println(it) }
  12. // SAM-type button.setOnClickListener {} // Multiple textView.addTextChangedListener(object: TextWatcher { override

    fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {} override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {} override fun afterTextChanged(s: Editable?) {} }) Lambdas android docs
  13. trait Clickable { val enabled: Boolean // Propriedade fun onClick()

    // Método fun click() { // Implementado if (enabled) { onClick() } } } Traits docs
  14. class Button : Clickable { override val enabled: Boolean =

    true override fun onClick() { println("Click!") } } Button().click() // "Click!" Traits docs
  15. fun String.first(): Char { return this[0] } // Use val

    text = "Hey!" println(text.first()) //"H" Extensions docs
  16. When docs fun foo(obj: Any) { when (obj) { 1

    -> println("One") "Hello" -> println("Greeting") is Long -> println("Long") !is String -> println("Not a string") else -> println("Unknown") } } foo(1) // "One"
  17. - Auto-Complete um pouco mais lento - Quase versão final

    - Problema com permissão de arquivos - Auto-Complete de nome de variável: val user = User("Luke", "Skywalker") Outros
  18. Android Studio 1.2 - Preferences > Plugins - Browse repositories…

    - Procurar: "Kotlin" - Instalar - Reiniciar Android Studio (2015?) 1. Instalar Plugin "Kotlin"
  19. 2. Configurar Gradle Plugin Editar: build.gradle buildscript { dependencies {

    classpath 'com.android.tools.build:gradle:1.2.2' classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:+' } }
  20. 3. Configurar Dependência Editar: app/build.gradle apply plugin: 'com.android.application' apply plugin:

    'kotlin-android' [...] dependencies { [...] compile 'org.jetbrains.kotlin:kotlin-stdlib:+' }
  21. Kotlin Reference Kotlin Project Kotlin Blog Kotlin Project Using Project

    Kotlin for Android Jake Wharton One month with Kotlin Ademar Oliveira Referências