Kotlin from scratch
franco.lombardo@smeup.com - https://www.linkedin.com/in/francolombardo/
Kotlin
The five Wh questions
• What?
• Who?
• When?
• Where?
• Why?
What is Kotlin?
Kotlin is a programming language
with a syntax similar to C / Java / JavaScript / C#
• Strong typing (a number is not a string)
• Static typing (types of variables are checked at
compile time)
• Type inference (type declaration can often be omitted)
• Object Oriented Programming (classes, objects,
methods, inheritance, polymorphic calls)
• Functional Programming Paradigm (functions are
types, so we can pass them around)
Who created Kotlin?
• Kotlin was created by JetBrains, a company that
builds programming languages IDEs and tools
such as IntelliJ IDEA
• There is a Kotlin Foundation created with Google to
protect the trademarks and to design the development
path
• Open source licensed under Apache 2
• Kotlin is the name of an island near St. Petersburg
(Java is the name of an island, too)
When was Kotlin created?
• 2010: Start of the Kotlin project
• 2012: The project becomes Open Source
• 2016: First stable release
• 2017: Google announces support on Android
• 2019: It becomes the preferred language for Android
Where does Kotlin run?
• JVM: primary target, the only one that is battle-proven
• JS: browser and server (NodeJS) (early stage)
• Native: iOS and more (early stage)
Be careful: cross platform code cannot use Java libraries!
Why Kotlin?
• It’s modern (OOP + functional + coroutines +…)
• It’s concise
• It’s interoperable
• it can reuse Java libraries ecosystem
• it can be used by Java projects
• It’s multiplatform (maybe?)
• It handles null values in a safer way
• Decoupling the evolution of the language from the
evolution of the JVM (e.g. phones, AS400, mission
critical production servers)
Why Kotlin?
Working in Kotlin is a lot of fun!
J
The very recommended IDE for Kotlin
The very recommended IDE for Kotlin
The very recommended IDE for Kotlin
The very recommended
building system for Kotlin
Sample project: https://github.com/f-lombardo/kotlin-from-scratch
Strong static typing with inference
fun main() {
val authorOfNabucco: String = "Giuseppe Verdi"
val authorOfTurandot = "Giacomo Puccini"
println("Italian composers: $authorOfNabucco and $authorOfTurandot")
}
Strong static typing with inference
fun main() {
val authorOfNabucco: String = "Giuseppe Verdi"
val authorOfTurandot = "Giacomo Puccini"
println("Italian composers: $authorOfNabucco and $authorOfTurandot")
}
Explicit type declaration
Type inference
String
interpolation
Immutable variables are preferred
Multiline strings: Kotlin is concise
Test blocks of RPG CODE easily
Kotlin is concise: why is it important?
Concise
↓
Less boilerplate code
↓
Easier to understand
(less cognitive overload)
↓
Fewer bugs
How much is Kotlin concise?
Data class: Java Bean with
hashCode, toString, equals and much more
How much is Kotlin concise?
Decompiled Java version
of the previous data class
(just a part of it)
Default arguments
Default arguments = less overloading!!!
Kotlin is safer
Can be null
Cannot be null
val italianComposer: String = "Giuseppe Verdi"
val dutchComposer: String? = null
val result =
openDatabase("franco", "secret")
?.findFirstComposerByNation("Italy")
?.findOperaByYear(1871)
?: "No results"
Kotlin is safer
Composing nullable values/functions
in a safe way
Possibly null
Not null
val result =
openDatabase("franco", "secret")
?.findFirstComposerByNation("Italy")
?.findOperaByYear(1871)
?: "No results"
Kotlin is safer
Composing nullable values/functions
in an expressive way
“or else”“and then”
Extension functions
data class Database(val composers: List<Composer>)
data class Composer(val name: String,
val nation: String,
val operas: List<Opera>)
data class Opera(val name: String, val yearOfComposition: Int)
fun Database.findFirstComposerByNation(nation: String): Composer? =
this.composers.firstOrNull { it.nation == nation }
fun Composer.findOperaByYear(year: Int): Opera? =
this.operas.firstOrNull { it.yearOfComposition == year }
Extending classes from outside
Extension functions
Code organization based on domain aspects
Extension methods for domain objects
regarding the “SQL aspect”
The power of lambdas
val elapsedTime = measureTimeMillis {
execute(compilationUnit.main.stmts)
}
public fun measureTimeMillis(block: () -> Unit): Long {
val start = System.currentTimeMillis()
block()
return System.currentTimeMillis() - start
}
References
Kotlin in action
Dmitry Jemerov, Svetlana Isakova
Kotlin for Java Developers
https://www.coursera.org/learn/kotlin-for-java-developers
JetBrains Kotlin Academy
https://hyperskill.org/onboarding/?track=kotlin
JetBrains Kotlin playlist on YouTube
https://bit.ly/kotlinTube
Kotlin hands-on labs (Coroutines, JS, Native)
https://play.kotlinlang.org/hands-on/overview

Kotlin from-scratch

  • 1.
    Kotlin from scratch franco.lombardo@smeup.com- https://www.linkedin.com/in/francolombardo/
  • 2.
    Kotlin The five Whquestions • What? • Who? • When? • Where? • Why?
  • 3.
    What is Kotlin? Kotlinis a programming language with a syntax similar to C / Java / JavaScript / C# • Strong typing (a number is not a string) • Static typing (types of variables are checked at compile time) • Type inference (type declaration can often be omitted) • Object Oriented Programming (classes, objects, methods, inheritance, polymorphic calls) • Functional Programming Paradigm (functions are types, so we can pass them around)
  • 4.
    Who created Kotlin? •Kotlin was created by JetBrains, a company that builds programming languages IDEs and tools such as IntelliJ IDEA • There is a Kotlin Foundation created with Google to protect the trademarks and to design the development path • Open source licensed under Apache 2 • Kotlin is the name of an island near St. Petersburg (Java is the name of an island, too)
  • 5.
    When was Kotlincreated? • 2010: Start of the Kotlin project • 2012: The project becomes Open Source • 2016: First stable release • 2017: Google announces support on Android • 2019: It becomes the preferred language for Android
  • 6.
    Where does Kotlinrun? • JVM: primary target, the only one that is battle-proven • JS: browser and server (NodeJS) (early stage) • Native: iOS and more (early stage) Be careful: cross platform code cannot use Java libraries!
  • 7.
    Why Kotlin? • It’smodern (OOP + functional + coroutines +…) • It’s concise • It’s interoperable • it can reuse Java libraries ecosystem • it can be used by Java projects • It’s multiplatform (maybe?) • It handles null values in a safer way • Decoupling the evolution of the language from the evolution of the JVM (e.g. phones, AS400, mission critical production servers)
  • 8.
    Why Kotlin? Working inKotlin is a lot of fun! J
  • 9.
    The very recommendedIDE for Kotlin
  • 10.
    The very recommendedIDE for Kotlin
  • 11.
    The very recommendedIDE for Kotlin
  • 12.
    The very recommended buildingsystem for Kotlin Sample project: https://github.com/f-lombardo/kotlin-from-scratch
  • 13.
    Strong static typingwith inference fun main() { val authorOfNabucco: String = "Giuseppe Verdi" val authorOfTurandot = "Giacomo Puccini" println("Italian composers: $authorOfNabucco and $authorOfTurandot") }
  • 14.
    Strong static typingwith inference fun main() { val authorOfNabucco: String = "Giuseppe Verdi" val authorOfTurandot = "Giacomo Puccini" println("Italian composers: $authorOfNabucco and $authorOfTurandot") } Explicit type declaration Type inference String interpolation Immutable variables are preferred
  • 15.
    Multiline strings: Kotlinis concise Test blocks of RPG CODE easily
  • 16.
    Kotlin is concise:why is it important? Concise ↓ Less boilerplate code ↓ Easier to understand (less cognitive overload) ↓ Fewer bugs
  • 17.
    How much isKotlin concise? Data class: Java Bean with hashCode, toString, equals and much more
  • 18.
    How much isKotlin concise? Decompiled Java version of the previous data class (just a part of it)
  • 19.
  • 20.
    Kotlin is safer Canbe null Cannot be null val italianComposer: String = "Giuseppe Verdi" val dutchComposer: String? = null
  • 21.
    val result = openDatabase("franco","secret") ?.findFirstComposerByNation("Italy") ?.findOperaByYear(1871) ?: "No results" Kotlin is safer Composing nullable values/functions in a safe way Possibly null Not null
  • 22.
    val result = openDatabase("franco","secret") ?.findFirstComposerByNation("Italy") ?.findOperaByYear(1871) ?: "No results" Kotlin is safer Composing nullable values/functions in an expressive way “or else”“and then”
  • 23.
    Extension functions data classDatabase(val composers: List<Composer>) data class Composer(val name: String, val nation: String, val operas: List<Opera>) data class Opera(val name: String, val yearOfComposition: Int) fun Database.findFirstComposerByNation(nation: String): Composer? = this.composers.firstOrNull { it.nation == nation } fun Composer.findOperaByYear(year: Int): Opera? = this.operas.firstOrNull { it.yearOfComposition == year } Extending classes from outside
  • 24.
    Extension functions Code organizationbased on domain aspects Extension methods for domain objects regarding the “SQL aspect”
  • 25.
    The power oflambdas val elapsedTime = measureTimeMillis { execute(compilationUnit.main.stmts) } public fun measureTimeMillis(block: () -> Unit): Long { val start = System.currentTimeMillis() block() return System.currentTimeMillis() - start }
  • 26.
    References Kotlin in action DmitryJemerov, Svetlana Isakova Kotlin for Java Developers https://www.coursera.org/learn/kotlin-for-java-developers JetBrains Kotlin Academy https://hyperskill.org/onboarding/?track=kotlin JetBrains Kotlin playlist on YouTube https://bit.ly/kotlinTube Kotlin hands-on labs (Coroutines, JS, Native) https://play.kotlinlang.org/hands-on/overview