Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Kotlin
1. { Kotlin }
Statically typed language for the JVM,
Android and the browser.
Developed by JetBrains
First stable release February 15, 2016.
2. { Syntax }.var and val
var <propertyName>: <Type> = [property initializer]
get() // optional
set() // optional
//example
var str: String = “kotlin is awesome"
OR
var str = “kotlin is awesome"
3. { Syntax }.method declaration
fun <methodName> : <ReturnType>
//example
fun saySomthing() : String {
return “kotlin is awesome"
}
OR
fun saySomthing() = “kotlin is awesome"
4. { Syntax }
when (x) {
in 1..10 -> print("x is in the range")
in validNumbers -> print("x is valid")
!in 10..20 -> print("x is outside the range")
else -> print("none of the above")
}
5. { Syntax }
for ((k, v) in map) {
println("$k -> $v")
}
someObject?.let {
// execute this block if not null
}
fun foo(param: Int) {
val result = if (param == 1) {
"one"
} else if (param == 2) {
"two"
} else {
"three"
}
}
6. { Syntax }.inheritance
all classes by default are finals
open class Base {
open fun v() {}
fun nv() {}
}
class Derived() : Base() {
override fun v() {}
}
7. { Syntax }.inheritance
open class A {
open fun f() { print("A") }
fun a() { print("a") }
}
interface B {
fun f() { print("B") }
fun b() { print("b") }
}
class C() : A(), B {
// The compiler requires f() to be overridden:
override fun f() {
super<A>.f() // call to A.f()
super<B>.f() // call to B.f()
}
}
8. { Syntax }.inner classes
class A { // implicit label @A
inner class B { // implicit label @B
fun foo() {
val a = this@A // A's this
val b = this@B // B's this
}
}
}
9. Billion-dollar
mistake
Tony Hoare
My goal was to ensure that all use of references should
be absolutely safe, with checking performed automatically
by the compiler. But I couldn't resist the temptation to put
in a null reference, simply because it was so easy to
implement.
10. { null safety } nullable and non-nullable types
var str: String = “kotlin is awesome"
str = null // compilation error… str is non-nullable type
var str: String? = “kotlin is awesome"
str = null // ok... str now can be null
val length = name.length // compilation error
val length = name?.length // ok
11. { null safety } nullable and non-nullable types
var name: String? = “some string"
if (name != null){
length = name.length
}else{
length = 0
}
or more shortly
val length = name?.length ?: 0
12. { Default parameters }
fun tmpMethod(a: Int = -10, b: Int = 5, c: Int = 0){
print(a + b + c)
}
----------------------------------------------------------------
tmpMethod(1) // 7
tmpMethod(2) // -8
tmpMethod(1,2,3) // 6
tmpMethod(b = 0, a = 2) // 2
20. { Extensions }.methods
var myObject : MyClass? = null
if (myObject.isNull()){
// my object is null
}
fun MyClass?.isNull() : Boolean {
return this == null
}
OR
fun MyClass?.isNull() = this == null
21. { Extensions }.methods
fun Activity.toast(msg: String, toastLength: Int = Toast.LENGTH_SHORT) {
Toast.makeText(this, msg, toastLength).show()
}
fun Fragment.toast(msg: String, toastLength: Int = Toast.LENGTH_SHORT) {
Toast.makeText(this, msg, toastLength).show()
}
22. { Extensions }.properties
val myList = listOf(1,2,3,4,5)
val lastIndex = myList.lastIndex // lastIndex = 4
--------------------------------
23. { Extensions }.properties
val myList = listOf(1,2,3,4,5)
val lastIndex = myList.lastIndex // lastIndex = 4
--------------------------------
val <T> List<T>.lastIndex: Int
get() = size - 1
24. { Lambdas }
Collections.sort(items, { t1, t2 -> t1.name.compareTo(t2.name)})
---------------------------------------------------------------------------------------------------------
in java
Collections.sort(items, new Comparator<MyClass>() {
@Override
public int compare(MyClass t1, MyClass t2) {
return t1.getName().compareTo(t2.getName());
}
});
25. { Higher-Order funcitons}
fun <T, R> List<T>.map(transform: (T) -> R): List<R> {
val result = arrayListOf<R>()
for (item in this)
result.add(transform(item))
return result
}
val doubled = items.map { it * 2 }
fun <T> max(collection: Collection<T>,
less: (T, T) -> Boolean): T? {
var max: T? = null
for (it in collection)
if (max == null || less(max, it))
max = it
return max
}
val less = {a:Int,b:Int -> a > b}
26. Example of SqliteDatabase transaction in android
------------------------------------------------------------------------------------
db.beginTransaction()
try {
db.delete("tableName","firstName =?", arrayOf("Jake"))
db.setTransactionSuccessful()
}finally {
db.endTransaction()
}
27. Example of SqliteDatabase transaction in android
------------------------------------------------------------------------------------
db.beginTransaction()
try {
// do work here
db.setTransactionSuccessful()
}finally {
db.endTransaction()
}