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

Lets talk in / Dilo en Kotlin

Daniel
October 06, 2016

Lets talk in / Dilo en Kotlin

Talk about Kotlin basics for:

- http://pataconf.com/
- Meetup Medellin Java https://www.meetup.com/es-ES/medellin-java/events/235359537

Daniel

October 06, 2016
Tweet

More Decks by Daniel

Other Decks in Programming

Transcript

  1. Lets talk in / Dilo en Daniel Gomez Rico Android

    Lead Developer at Barista Ventures +danielgomezrico @aquicaipivara https://github.com/JetBrains/kotlin v 1.0.4
  2. Designed to avoid common problems NullPointerException Transactions Ceremonies 
 (begin(),

    do(t), finish()) Casting hashCode(), equals(), Y toString() default arguments named arguments Inmutable
 Mutable
 Collections StringBuilder
  3. Release History february 15 1.0.0 September 22 1.0.4 June 30

    1.0.3 May 13 1.0.2 March 16 1.0.1 2010 2016 Nov 8 1.0.5
  4. Elvis operator
 o null-coalescing operator If left is null then

    use right value ?: fun showDialog(text: String?) = print(text ?: "Empty")
  5. Elvis operator
 o null-coalescing operator If left is null then

    use right value ?: fun showDialog(text: String?) = print(text ?: "Empty")
  6. Elvis operator
 o null-coalescing operator If left is null then

    use right value ?: fun showDialog(text: String?) = print(text ?: "Empty")
  7. Elvis operator
 o null-coalescing operator If left is null then

    use right value ?: fun showDialog(text: String?) = print(text ?: "Empty")
  8. class Person(val name: String, var age: Int)
 
 val person

    = Person("Clark Kent", age = 1)
 print("${person.name} ${person.age}") Clark Kent 1
  9. data class Person(val name: String, var age: Int)
 
 val

    person = Person("Clark Kent", age = 1)
 print(person) Person(name=Clark Kent, age=1)
  10. interface Operation
 
 interface Transactor {
 fun begin()
 fun finish()


    fun add(operation: Operation)
 } val transactor = TransactorImpl()
 
 transactor.begin()
 transactor.add(Operation())
 transactor.finish()
  11. interface Operation
 
 interface Transactor {
 fun begin()
 fun finish()


    fun add(operation: Operation)
 } val transactor = TransactorImpl()
 
 transactor.begin()
 transactor.add(Operation())
 transactor.finish()
  12. interface Operation
 
 interface Transactor {
 fun begin()
 fun finish()


    fun add(operation: Operation)
 } val transactor = TransactorImpl()
 
 transactor.begin()
 transactor.add(Operation())
 transactor.finish()
  13. fun Transactor.safeRun(func: () -> Unit) {
 begin()
 func()
 finish()
 }

    val transactor = TranstorImpl()
 transactor.safeRun({ transactor.add(Operation()) })
  14. fun Transactor.safeRun(func: () -> Unit) {
 begin()
 func()
 finish()
 }

    val transactor = TranstorImpl()
 transactor.safeRun { transactor.add(Operation()) }
  15. fun Transactor.safeRun(func: () -> Unit) {
 begin()
 func()
 finish()
 }

    val transactor = TranstorImpl()
 transactor.safeRun { transactor.add(Operation()) }
  16. fun Transactor.safeRun(func: () -> Unit) {
 begin()
 func()
 finish()
 }

    val transactor = TranstorImpl()
 transactor.safeRun { transactor.add(Operation()) }
  17. fun Transactor.safeRun(func: Transactor.() -> Unit) {
 begin()
 func()
 finish()
 }

    val transactor = TranstorImpl()
 transactor.safeRun { add(Operation()) }
  18. fun main(args: Array<String>) {
 val age = 2
 val name

    = "Superman"
 val text = "$name $age"
 
 print(text)
 } Kotlin
  19. fun main(args: Array<String>) {
 val age = 2
 val name

    = "Superman"
 val text = "$name $age"
 
 print(text)
 } Kotlin
  20. L3 LINENUMBER 43 L3 NEW java/lang/StringBuilder DUP INVOKESPECIAL java/lang/StringBuilder.<init> ()V

    ALOAD 2 INVOKEVIRTUAL java/lang/StringBuilder.append (Ljava/lang/String;)Ljava/lang/ StringBuilder; LDC " " INVOKEVIRTUAL java/lang/StringBuilder.append (Ljava/lang/String;)Ljava/lang/ StringBuilder; ILOAD 1 INVOKEVIRTUAL java/lang/StringBuilder.append (I)Ljava/lang/StringBuilder; INVOKEVIRTUAL java/lang/StringBuilder.toString ()Ljava/lang/String; ASTORE 3 ByteCode fun main(args: Array<String>) {
 val age = 2
 val name = "Superman"
 val text = "$name $age"
 
 print(text)
 } Kotlin
  21. import kotlin.Metadata;
 import kotlin.jvm.internal.Intrinsics;
 import org.jetbrains.annotations.NotNull;
 
 public final class

    LiveKt {
 public static final void main(@NotNull String[] args) {
 Intrinsics.checkParameterIsNotNull(args, "args");
 byte age = 2;
 String name = "Superman";
 (new StringBuilder()).append(name).append(" ").append(age).toString();
 }
 } Java fun main(args: Array<String>) {
 val age = 2
 val name = "Superman"
 val text = "$name $age"
 
 print(text)
 } Kotlin
  22. Caution 1 Kotlin Java List<String> list = findNames();
 list.add("thing"); fun

    findNames(): List<String> {
 //...
 } val list = findNames()
 list.add("thing")
  23. Caution 2 Kotlin fun findNames(): MutableList<String> {
 //...
 } Java

    List<String> list = findNames();
 list.add(null); val list = findNames()
 list.add(null)
  24. Reference Google images… iconfinder.com Jake Wharton - Android Development with

    Kotlin Jake Wharton - Advancing Android Development with the Kotlin Language Better Android Development with Kotlin and Gradle by Ty Smith & Yohan Hartanto