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

Kotlin why you should consider using it

Kotlin why you should consider using it

Souleymane Sidibe

December 16, 2017
Tweet

More Decks by Souleymane Sidibe

Other Decks in Programming

Transcript

  1. • Quick introduction to Kotlin • Features that I love

    • Kotlin for Android developers • What else? Agenda
  2. String interpolation val x = 4 val y = 7

    print("sum of $x and $y is ${x + y}”) "// sum of 4 and 7 is 11
  3. Type inference val a = "abc" "// type inferred to

    String val b = 4 "// type inferred to Int val c: Double = 0.7 "// type declared explicitly val d: List<String> = ArrayList() "// type declared explicitly
  4. Default Arguments fun build(title: String, width: Int = 800, height:

    Int = 600) { Frame(title, width, height) } fun main(args: Array<String>) { build("Title") build("Title", 1024, 1024) build("Title", height = 1024) }
  5. Data Class data class Person(val name: String, var email: String,

    var age: Int) val person = Person("Abdou", "[email protected]", 20) "//Person(name=Abdou, [email protected], age=20)
  6. Better for loop for (i in 1"..100) { ""... }

    for (i in 0 until 100) { ""... } for (i in 2"..10 step 2) { ""... } for (i in 10 downTo 1) { ""... }
  7. Better collections api val list: List<Int> = listOf<Int>(1, 2, 3,

    4) mapOf<String, Int>("Mouhamed" to 21, "ali" to 25, "Papi" to 29) setOf<String>("one", "two", "three") val mutableList: MutableList<Int> = mutableListOf(1, 2, 3, 4) mutableMapOf("Mouhamed" to 21, "ali" to 25, "Papi" to 29) mutableSetOf("one", "two", "three") val list = listOf(1, 2, 3, 4) val newList = list.map { it * 2 }.filter { it > 2 }
  8. Null safety • NullPointerException Problem • Nullable Type, Non Nullable

    Type fun main(args: Array<String>) { var name: String = “Soulesidibe" }
  9. Null safety • NullPointerException Problem • Nullable Type, Non Nullable

    Type fun main(args: Array<String>) { var name: String? = “Soulesidibe" }
  10. Null safety • NullPointerException Problem • Nullable Type, Non Nullable

    Type fun main(args: Array<String>) { var name: String? = “Soulesidibe” val length = name?.length }
  11. Null safety • NullPointerException Problem • Nullable Type, Non Nullable

    Type fun main(args: Array<String>) { var name: String? = “Soulesidibe” val length = if (name "== null) 0 else name.length }
  12. Extension functions • Extend a class without modifying it •

    Avoid utils classes fun String.hello(): String { return “Hello $this” } fun main(args: Array<String>) { var name: String = “Soulesidibe” println(name.hello()) }
  13. Extension functions • Extend a class without modifying it •

    Avoid utils classes fun MutableList<Int>.swap(index1: Int, index2: Int) { val tmp = this[index1] this[index1] = this[index2] this[index2] = tmp } fun main(args: Array<String>) { var list = mutableListOf(1, 2, 3, 5) list.swap(1,2) "//1,3,2,5 }
  14. High order functions • A function that takes a function

    as parameter • A function that returns a function fun operation(a: Int, b: Int, func: (Int,Int) "-> Int): Int { return func(a, b) } fun main(args: Array<String>) { operation(3, 4, {a, b "-> a + b}) }
  15. High order functions • A function that takes a function

    as parameter • A function that returns a function fun operation(a: Int, b: Int, func: (Int,Int) "-> Int): Int { return func(a, b) } fun main(args: Array<String>) { operation(3, 4) {a, b "-> a + b} }
  16. Class Delegation •Design pattern in OOP •Object composition without inheritance

    •Native support on Kotlin interface Base { fun print() } class BaseImpl(val x: Int) : Base { override fun print() { print(x) } } class Derived(val x: Int) : Base by BaseImpl(x) fun main(args: Array<String>) { Derived(10).print() "// prints 10 }
  17. Class Delegation •Design pattern in OOP •Object composition without inheritance

    •Native support on Kotlin interface Base { fun print() } class BaseImpl(val x: Int) : Base { override fun print() { print(x) } } class Derived(b: Base) : Base by b fun main(args: Array<String>) { val b = BaseImpl(10) Derived(b).print() "// prints 10 }
  18. Kotlin for android developers • Google announces official support on

    android • Android Studio supports Kotlin • Jake Wharton joins Google • The community adopts Kotlin
  19. Interoperability • 100% compatible with java 6, 7, 8, 9

    • Kotlin and Java interoperable class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) } }
  20. Be lazy • Value gets computed only upon first access

    • Better memory management(Heap) • Less garbage collection class MainActivity : AppCompatActivity() { val button: Button by lazy { findViewById<Button>(R.id.btn_click) } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) button.setOnClickListerner{ "// code… } } }
  21. Extension functions • Avoid API version check • More concise

    code class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) if (button.isAttachedWindow()) { "// coded } } } fun View.isAttachedWindow(): Boolean { return if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { ViewCompat.isAttachedToWindow(this) } else isAttachedToWindow }
  22. Extension functions • Avoid boilerplate • More concise code "//

    java String firstName = null; int firsNameColumn = cursor.getColumnIndexOrThrow("first_name"); if (!cursor.isNull(firsNameColumn)) { firstName = cursor.getString(firsNameColumn); }
  23. Extension functions • Avoid boilerplate • More concise code "//

    java String firstName = null; int firsNameColumn = cursor.getColumnIndexOrThrow("first_name"); if (!cursor.isNull(firsNameColumn)) { firstName = cursor.getString(firsNameColumn); } "// kotlin fun Cursor.getStringOrNull(columnName: String): String? { val index = getColumnIndexOrThrow(columnName) return if (isNull(index)) null else getString(index) }
  24. Extension functions • Avoid boilerplate • More concise code "//

    java String firstName = null; int firsNameColumn = cursor.getColumnIndexOrThrow("first_name"); if (!cursor.isNull(firsNameColumn)) { firstName = cursor.getString(firsNameColumn); } "// kotlin fun Cursor.getStringOrNull(columnName: String): String? { val index = getColumnIndexOrThrow(columnName) return if (isNull(index)) null else getString(index) } val name = cursor.getStringOrNull("first_name") firstName.text = name "?: "Soulesidibe"
  25. What else? • Not just for android developers • Kotlin

    for server side(Spring, Ktor, Vertx, Spark) • Kotlin/Js (client and server) • Kotlin/native "=> Code sharing
  26. Where to learn Kotlin • Official Kotlin Doc • Jake

    Wharton talk • Antonio Leiva Blog • My blog • Try it online