The 🇨🇭 army knife of
programming languages?
Tudor Dragan
Senior Mobile Developer
Hello 👋
Welcome to 🇱🇹
About me
• Senior Mobile Developer @ Visma e-conomic
👨💻📱
• Passion for UX and design 👨🎨
• Romanian 🇷🇴 living in Copenhagen 🇩🇰
• I ❤ emojis
• 🐈 person
100%
Agenda for today 📋
• Intro 👶
• Optionals?
• Data Classes vs POJOs vs Structs
• Standard functions
• Unlock the functional power ⚡
• Delegation 🤝
• Sealed classes
• Glimpse into the future 🔮
Intro 👶
Kotlin
• Statically typed programming language
• Runs on the Java virtual machine
• Can be compiled to JavaScript source code 
• Also can be compiled to native binaries that run
without any VM (more on that later…)
It’s 100%
interoperable with Java
Comparing to
–Sir Charles Antony Richard Hoare
“I call it my billion-dollar mistake.”
NullPointerException
Kotlin’s solution
• Integrate nullability in the type system
• Equivalent to Optionals in Swift
• Introduces the notion of Safe calls and Safe
casts
The Optional? type
Demo
👨💻
String? ≠ String
–Java developer
“Why not just use @Nullable or @NotNull annotations?”
Java 8 features (subset) availability
< 50%
Official Android Distribution Chart - August 2018
Data Class
vs
POJO
vs
Swift Struct
POJO vs Data Class
data class Person(var name: String, var age: Int)public class Person {
private String name;
private int age = 0;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
Data Class
Advantages
• The compiler automatically derives
members from properties
• Support destructuring declarations so it’s
convenient to destructure an object into
a number of variables
fun equals()
fun hashCode()
// will output "User(name=John, age=42)";
fun toString()
// functions corresponding to the properties in their
order of declaration;
fun componentN()
// for when we need to copy an object altering some of
its properties, but keeping the rest unchanged.
fun copy(name: String = this.name, age: Int = this.age)
= User(name, age)
// destructing declarations
val jane = User("Jane", 35)
val (name, age) = jane
println("$name, $age years of age") // prints "Jane, 35
years of age"
Data Class
Requirements
• The primary constructor needs to have at least one parameter;
• All primary constructor parameters need to be marked as val or var;
• Data classes cannot be abstract,open,sealed or inner;
Data Class
vs
Swift Struct
Swift Struct
• A value type is a type whose value
is copied when it’s assigned to a variable or
constant,or when it’s passed to a function.
• All structures and enumerations are value
types in Swift.
• This means that any structure and enumeration
instances you create—and any value types they
have as properties—are always copied when
they are passed around in your code.
From the Swift Docs
Data Class
• From the name,it’s a class
• Java manipulates objects by reference,and all object
variables are references. Only primitives are assignable.
• But we have the generated copy() method right? 🤔
From the Kotlin Docs
Example
👨💻
copy() ≠ clone()
❤
❤
Functional Programming
Standard.kt
/**
* Standard.kt
*/
public inline fun <T, R> T.let(block: (T) -> R): R
public inline fun <T> T.also(block: (T) -> Unit): T
public inline fun <T> T.apply(block: T.() -> Unit): T
public inline fun <R> run(block: () -> R): R
public inline fun <T, R> T.run(block: T.() -> R): R
public inline fun <T, R> with(receiver: T, block: T.() -> R): R
} Unwrapping
variables
Demo
👨💻
Standard Functions Cheat Sheet
Function Receiver (this) Argument (it) Result
let this@Class String(“…”) Int(13)
also this@Class String(“…”) String(“…”)
run String(“…”) N  A Int(13)
run* this@Class N  A Int(13)
apply String(“…”) N  A String(“…”)
with* String(“…”) N  A Int(13)
* those functions are not extension methods, they need to be called the old-fashioned way
Taken from Tomek Polański's blog post

https://bit.ly/2MtqPOg
λ expressions
Short Demo
👨💻
Delegation
Delegation pattern
• has proven to be a good alternative to
implementing inheritance
• supported natively
• no boilerplate code
Delegated properties
lazy()
val lazyValue: String by lazy {
print("Computed ")
"Hello!"
}
fun main(args: Array<String>) {
println(lazyValue) // Prints "Computed Hello!"
println(lazyValue) // Prints "Hello!"
}
Delegated properties
observable()
class User {
var name: String by observable("<no name>") {
prop, old, new ->
println("$old -> $new")
}
}
fun main(args: Array<String>) {
val user = User()
user.name = “first" // <no name> -> first
user.name = “second" // first -> second
}
Delegated properties
map()
class User(map: Map<String, Any?>) {
val name: String by map
val age: Int by map
}
val user = User(
mapOf(
"name" to "Tudor",
"age" to 27
)
)
fun main(args: Array<String>) {
println(user.name) // Prints "Tudor"
println(user.age) // Prints 27
}
Delegated properties
Imagination()
Demo
👨💻
Sealed Classes
• Used for representing restricted class hierarchies
• An extension of enum classes
• Can contain state
What are they?
• Filtering on collections can be done using the query string
parameter filter
• A filter is made up of a set of predicates,and follows a syntax
inspired by mongoDB.
• A predicate is made up of a property name,an operator
and a value.
e-conomic API - FilteringUse case
Example
?filter=name$eq:Joe
Property Operator Value
Example
?filter=name$eq:Joe$and:(city$like:*port$or:age$gt:40)
Predicate Operator Predicate
Demo
👨💻
Developer happiness
🤓
The future
Kotlin/Native
Kotlin/Native
• Compiling Kotlin to native binaries that run without any VM.
• It comprises a LLVM-based backend for the Kotlin compiler and a native
implementation of the Kotlin runtime library.
• Primarily designed to allow compilation for platforms where virtual
machines are not desirable or possible (such as iOS,embedded targets) or
to produce a reasonably-sized self-contained program that does not
require an additional runtime.
From the Kotlin Docs
Kotlin/Native
• Windows (x86_64 only at the moment)
• Linux (x86_64,arm32,MIPS,MIPS little
endian)
• MacOS (x86_64)
• iOS (arm32 and arm64)
• Android (arm32 and arm64)
• WebAssembly (wasm32 only)
From the Kotlin Docs
KoltinConf App KoltinConf Spinner App
+ = ❤?
Thank you!
@tudorgk
tudorgk
Q & A
kotlin
https://github.com/e-conomic/kotlin-mobile-workshop-vilnius-2018
@tudorgk
tudorgk

Kotlin - The Swiss army knife of programming languages - Visma Mobile Meet-up 2018 - Vilnius