DSC MUET Hello To Kotlin
DSC MUET Hello To Kotlin
@ArbazPirwani
life begins
at the end of your
COMFORT
ZONE
-Neale Donald Walsch-
Kick Start With Kotlin
Arbaz Pirwani
@arbazpirwani
Modern Language
Kotlin Timeline
1.0
Server
Kotlin/JVM
Android
Kotlin/JVM
iOS Browser
Kotlin/Native Kotlin/JS
Question Of The Day
Expressiveness Safety
Interoperability Structured
concurrency
Why Kotlin?
class MainActivity : AppCompatActivity () {
override fun onCreate(savedInstanceState: Bundle?) {
...
fab.backgroundTintList = backgroundTintList
}
}
Why Kotlin? Nullability in the
type system
class MainActivity : AppCompatActivity () {
override fun onCreate(savedInstanceState: Bundle?) {
...
fab.backgroundTintList = backgroundTintList
}
}
Why Kotlin? Nullability in the
type system
class MainActivity : AppCompatActivity () {
override fun onCreate(savedInstanceState: Bundle?) {
Lambdas
...
fab.backgroundTintList = backgroundTintList
}
}
Why Kotlin? Nullability in the
type system
class MainActivity : AppCompatActivity () {
override fun onCreate(savedInstanceState: Bundle?) {
Lambdas
...
Extension
fab.setOnClickListener { view -> functions
Snackbar.make(view,
"Hello ${name.capitalize()}",
Snackbar.LENGTH_LONG).show()
}
fab.backgroundTintList = backgroundTintList
}
}
Why Kotlin? Nullability in the
type system
class MainActivity : AppCompatActivity () {
override fun onCreate(savedInstanceState: Bundle?) {
Lambdas
...
Extension
fab.setOnClickListener { view -> functions
Snackbar.make(view,
"Hello ${name.capitalize()}",
Snackbar.LENGTH_LONG).show()
Template
}
expressions
fab.backgroundTintList = backgroundTintList
}
}
Why Kotlin? Nullability in the
type system
class MainActivity : AppCompatActivity () {
override fun onCreate(savedInstanceState: Bundle?) {
Lambdas
...
Extension
fab.setOnClickListener { view -> functions
Snackbar.make(view,
"Hello ${name.capitalize()}",
Snackbar.LENGTH_LONG).show()
Template
}
expressions
fab.backgroundTintList = backgroundTintList
}
}
Property access
syntax for getters
and setters
Kotlin Uptake
Coroutines
● ●
● Type Aliases ● Ranges
● Named Parameters ● Map Array Syntax
● Spread Operator ● Inline Functions
● Auto Casts ● Sealed Classes
● Covariance ● Delegation
● Null Safety ● Destructuring
● Operator Overloading ● KDoc
Coroutines
● No callbacks
● No complex functionality
VS
Kotlin & Swift
Swift
print("Hello, world!")
Kotlin
println("Hello, world!")
Swift is like Kotlin:
Variables And Constants
Swift
var myVariable = 42
myVariable = 50
let myConstant = 42
Kotlin
var myVariable = 42
myVariable = 50
val myConstant = 42
Swift is like Kotlin:
Explicit Types
Swift
let explicitDouble: Double = 42
Kotlin
val explicitDouble: Double = 42.0
Swift is like Kotlin:
Arrays
Swift
var shoppingList = ["catfish", "water",
"tulips", "blue paint"]
shoppingList[1] = "bottle of water"
Kotlin
val shoppingList = arrayOf("catfish", "water",
"tulips", "blue paint")
shoppingList[1] = "bottle of water"
Swift is like Kotlin:
Functions
Swift
func greet(_ name: String,_ day: String) -> String {
return "Hello \(name), today is \(day)."
}
greet("Bob", "Tuesday")
Kotlin
fun greet(name: String, day: String): String {
return "Hello $name, today is $day."
}
greet("Bob", "Tuesday")
Swift is like Kotlin:
Classes
Swift
class Shape {
var numberOfSides = 0
func simpleDescription() -> String {
return "A shape with \(numberOfSides) sides."
}
}
Kotlin
class Shape {
var numberOfSides = 0
fun simpleDescription()
= "A shape with $numberOfSides sides."
}
Java strikes back with Java 8
• Primary constructors
• No fields, just properties
• Bean style classes easy to declare
• By default, all classes are final
Null Safety
More elegant?
Tony Hoare
Generics
More elegant?
• Declaration-site variance
• out / in generics modifiers
• Supports use-site variance also
• No wildcard syntax, out / in used
MORE COOL STUFF
Default arguments and
named arguments
• https://kotlinlang.org/
• https://play.kotlinlang.org/
• http://nilhcem.com/swift-is-like-kotlin/
FreeCodeCamp.org
Android Developers
Best Kotlin Learning Resources
Arbaz Pirwani