Share
Embed
Kotlin why you should consider using it
Souleymane Sidibe
December 16, 2017
Transcript
35 slides
-
• Quick introduction to Kotlin • Features that I love
• Kotlin for Android developers • What else? Agenda -
Java interoperability • 100% interoperable with Java • Your framework
and libs are available -
String interpolation val x = 4 val y = 7
print("sum of $x and $y is ${x + y}”) "// sum of 4 and 7 is 11 -
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 -
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) } -
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) -
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) { ""... } -
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 } -
Null safety • NullPointerException Problem • Nullable Type, Non Nullable
Type fun main(args: Array<String>) { var name: String = “Soulesidibe" } -
Null safety • NullPointerException Problem • Nullable Type, Non Nullable
Type fun main(args: Array<String>) { var name: String? = “Soulesidibe" } -
Null safety • NullPointerException Problem • Nullable Type, Non Nullable
Type fun main(args: Array<String>) { var name: String? = “Soulesidibe” val length = name?.length } -
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 } -
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()) } -
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 } -
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}) } -
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} } -
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 } -
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 } -
Kotlin for android developers • Google announces official support on
android • Android Studio supports Kotlin • Jake Wharton joins Google • The community adopts Kotlin -
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) } } -
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… } } } -
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 } -
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); } -
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) } -
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" -
What else? • Not just for android developers • Kotlin
for server side(Spring, Ktor, Vertx, Spark) • Kotlin/Js (client and server) • Kotlin/native "=> Code sharing -
Where to learn Kotlin • Official Kotlin Doc • Jake
Wharton talk • Antonio Leiva Blog • My blog • Try it online