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

Introduction to Kotlin Workshop

Introduction to Kotlin Workshop

Introduction to Kotlin - Android; basics, features, issues, demo and workshop.

Akshay Chordiya

July 01, 2017
Tweet

More Decks by Akshay Chordiya

Other Decks in Programming

Transcript

  1. • A Computer program writer • Love writing tech blogs

    and contributing back to community • Mentor college graduates and professional who wants to go for mobile app development • Visit Hardik Trivedi GitHub Blog
  2. • Why Kotlin? • Problems with Java - Android •

    What’s Kotlin? • Syntax • Features • Demo • Issues • And finally… questions Agenda
  3. Problems with Java -- Android • Stuck at Java 6-7;

    Java 8 has now come to rescue • Restrictions to add functionality to Platform APIs • Android loves Inheritance • Lots of boilerplate • Ceremony of APIs • Android loves to throw nulls • Lack of Modern coding style • And so much more….
  4. Kotlin • Modern & Statically typed • Targets JVM, Android

    and JavaScript • Works everywhere where Java works • interoperable with Java™ • Combines Object Oriented and Functional features • Made by good folks at JetBrains • Open source
  5. // String (Implicitly inferred) val spell = "Lumos" // Int

    val number = 74 // Explicit type var spells: List<String> = ArrayList() Variables // String (Implicitly inferred) val spell = "Lumos" // Int val number = 74 // Explicit type var spells: List<String> = new ArrayList()
  6. “Use val for a value that won’t change” - Immutable

    val vs var “Use var for something that will vary with time.” - Mutable
  7. val max = if (a > b) a else b

    Control Flow val max = if (a > b) { a } else { b }
  8. when (x) { in 1..10 -> print("x is in") !in

    10..20 -> print("x is outside") else -> print("To infinity & beyond") } when expression when { x.isOdd() -> print("x is Odd") x.isEven() -> print("x is Even") else -> print("x is different") }
  9. // De-structured for ((index, value) in avengers.withIndex()) { println("$value at

    $index") } Loops val avengers = listOf("Iron Man","Thor") // For-each for (avenger in avengers) { println(avenger) }
  10. fun add(a: Int, b: Int): Int { return a +

    b } Functions fun add(a: Int, b: Int) = a + b fun add(a: Int = 0, b: Int = 0) = a + b
  11. class Spell(name: String = "") { var code: Int =

    0 constructor(name: String, code: Int): this(name) { this.code = code } } Class
  12. class Spell(name: String = "") { var code: Int =

    0 init { code = generateCode(name) } } Class
  13. Singleton in Kotlin object MagicWand { var spell: String? =

    null } // Class is initialized MagicWand.spell = "Alohomora" // Prints the value println(MagicWand.spell)
  14. // Compile time error val spell: String = null //

    OK val spell: String? = null Declaring null variables
  15. // Compile time error var spell: String = "Lumos" spell

    = null // OK var spell: String? = "Lumos" spell = null Even...
  16. // Compile time error val spell: String? = null val

    length = spell.length // OK val spell: String? = null if (spell != null) { // Smart cast val length = spell.length } Null Safety
  17. // Safe val spell: String? = null val length =

    spell?.length // Safe with else val length = spell?.length ?: -1 Kotlin-ized way
  18. String interpolation fun sayHello(message: String) { println("Welcome $message") } fun

    sayTime() { println("Time: ${System.currentTimeMillis()}") }
  19. fun consume(f: () -> Unit): Boolean { f() return true

    } Higher Order Functions inline fun consume(f: () -> Unit): Boolean { f() return true }
  20. fun View.visible() { visibility = VISIBLE } fun View.visible() {

    visibility = VISIBLE } fun String.isEmail(): Boolean { return Patterns.EMAIL_ADDRESS.matcher(this).matches() } fun String.isEmail(): Boolean { return Patterns.EMAIL_ADDRESS.matcher(this).matches() } Example: Extension functions
  21. val email = "[email protected]" if (email.isEmail()) { // Do login

    } Usage: Extension Function textView.visible()
  22. inline fun SharedPreferences. edit(func: SharedPreferences.Editor.() -> Unit) { val editor

    = edit() editor.func() editor.apply() } inline fun SharedPreferences. edit(func: SharedPreferences.Editor.() -> Unit) { val editor = edit() editor.func() editor.apply() } Extension + Higher Order functions // Usage val pref = PreferenceManager.getDefaultSharedPreferences(context) pref.edit{ putString("first_name", "Akshay") putString("last_name", "Chordiya") remove("last_name") }
  23. Smart Cast // Java way if (view instanceof TextView) {

    ((TextView) view).setText("Avifors"); } // Kotlin way if (view is TextView) { // Smart casting view.text = "Avifors" }
  24. with function with(textView) { text = "Accio" visible() } val

    textView = with(TextView(this)) { text = "Accio" visible() this }
  25. Issues • Slightly increased build time • Adds Dex Count

    (6K) to your app • No static methods and variables. The same functionality can be achieved using companion objects • Difficulty in coding in functional paradigm
  26. References • All the images and logos used are trademarks

    of respective companies. • Kotlin Official Website • Why Kotlin-ize your Android Development Medium - Akshay Chordiya • Antonio Leiva Blog • Why You Should Start Using Kotlin to Supercharge Your Android Development in 2017 • Android Development with Kotlin — Jake Wharton • Kotlin - Android document by Square • Droidcon NYC 2016 - Kotlin in Production • Being more productive with Kotlin