Thursday, July 11, 2019

Learning something new - Kotlin

Main: https://en.wikipedia.org/wiki/Kotlin_(programming_language)
Ref: https://medium.com/@mayuroks/learn-kotlin-for-android-in-one-day-50aa0bc099b0

Kotlin has been Google’s preferred language for Android app development since 7 May 2019.
I started working with Kotlin on 7 July 2019. That's 2 months of delay/gap.
I was busy traveling - Tokyo - Seattle - San Francisco - Tokyo - Mumbai - Delhi.

Now that i have settled in one place - Gurgaon, it's time to dive in.

History: Kotlin v1.0 was released on 15 February 2016.
It’s influenced by Java, Scala, Groovy, C#, Gosu, JavaScript, and Swift.

val : constant (immutable)
var : variable (mutable)

var username: String = "some user name"
val API_DELAY_CONSTANT: Int = 100

var myString: String? = null

fun getNumber(): Int {
return 1
}
private fun getStringLength(str1: String, str2: String): Int {
return str1.length + str2.length
}

var items: List<String> = ArrayList<String>()
for (item in items) {
// do something with item
}


for (index in items.indices) {
// access items using index
// items.get(index)
}


var i: Int = 0
// Some while loop
do {
    // do something
    i++

} while (i < 5)
// Another while loop
while (i < 10) {
    // do something
    i++


}

// A cool example of when statement
fun describe(obj: Any): String? {
var res: String? = null

when (obj) {
1 -> { res = "One" } // if obj == 1
"Hello" -> res ="Greeting" // if obj == "Hello"
is Long -> "Long" // if obj is of type Long
!is String -> "Not string" // if obj is not of type String
else -> {
// execute this block of code
}
}

return res
}



Kotlin has support for nested functions wow!

All nullable objects must be declared with a "?" postfix after the type name. Operations on nullable objects need special care from developers: null-check must be performed before using the value. Kotlin provides null-safe operators to help developers:
1. A safe navigation operator "?."
2. A null coalescing operator "?:" named Elvis operator after Elvis Presley


foo()?.bar()?.baz().

Android Studio (based on IntelliJ IDEA) has official support for Kotlin, starting from Android Studio 3.
link: https://developer.android.com/kotlin/index.html
Apps built with Kotlin:
  1. Slack
  2. Lyft
  3. Robinhood
  4. Evernote
  5. Square
  6. Deliveroo
  7. Periscope
  8. Udacity
  9. Instacart
  10. Pinterest
  11. Basecamp
  12. Pocket
  13. Reddit
  14. Camera360
  15. Trello
  16. Foursquare
  17. Stride
  18. Amex
  19. Expedia
  20. Netflix
  21. Kindle
  22. KeepSafe
  23. NYTimes
  24. Wordpress
  25. Simple Bank
  26. Twitter
  27. Airbnb
  28. Flipboard
  29. Foursquare Robin
  30. Tencent WeChat
  31. GoJek
  32. Confluence
  33. June
  34. Adobe Reader
  35. Zomato

30 minutes to Learn Kotlin : https://developer.android.com/kotlin/learn
Developer Story - Zomato : https://www.youtube.com/watch?v=ao9QvtpszOU

Going one step further with Kotlin & gRPC

Recently, I tried using Quarkus with Kotlin for grpc. I have worked with grpc for communication between microservices in Java & Golang. ...