training@instil.co
January 2019
© Instil Software 2018
Kotlin for All the Things
Kotlin Native
@BoyleEamonn
Eamonn Boyle
Me
Us
My Dog
Google Maps
2010: Work begins on Kotlin within JetBrains
2011: First public announcement on Kotlin
2012: Open sourced under Apache 2 license
2016: Version one released
2017: First class support on Android
2018: Version 1.3 released
A Kotlin Timeline
We’ve bought into Kotlin in a big way
Kotlin…Ok…we’re obsessed
We held a workshop at the conference
• React Web App with Kotlin on Server & Browser
Kotlin…Ok…we’re obsessed
If you’re working on the Java Virtual Machine, try not to use Java
Having done C# for many years I was so happy to see Kotlin for the JVM
Problems with Java
• Verbose, Verbose, Verbose, Verbose, Verbose, Verbose, Verbose, Verbose,
• Nulls
• Completely OO
Why Koltin - Java – Yuck!
No need to wait - the interop story is so good
• Call into all that legacy Java code easily
• Make calls into your new Kotlin code from Java easily
Really concise, yet clear syntax
• Less is more
• “Borrows” the best bits of other languages
• Less baggage
Why Kotlin – So Much to Like
Null Safety
String Templates
Default parameters
Extensions
Free Functions
Coroutines
Single Expression Functions
Reified generics
Data classes and Properties
Type Inference
Smart Casts
Operator overloading
Basic Model Classes – Java
public class Movie {
private String title;
private String description;
private Rating rating;
private Genre genre;
}
Basic Model Classes – Java
public class Movie {
private String title;
private String description;
private Rating rating;
private Genre genre;
public Movie(String title, String description, Rating rating, Genre genre) {
this.title = title;
this.description = description;
this.rating = rating;
this.genre = genre;
}
}
Basic Model Classes – Java
public class Movie {
private String title;
private String description;
private Rating rating;
private Genre genre;
public Movie(String title, String description, Rating rating, Genre genre) {
this.title = title;
this.description = description;
this.rating = rating;
this.genre = genre;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
Basic Model Classes – Java
public class Movie {
private String title;
private String description;
private Rating rating;
private Genre genre;
public Movie(String title, String description, Rating rating, Genre genre) {
this.title = title;
this.description = description;
this.rating = rating;
this.genre = genre;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Rating getRating() {
return rating;
}
public void setRating(Rating rating) {
this.rating = rating;
}
public Genre getGenre() {
return genre;
}
public void setGenre(Genre genre) {
this.genre = genre;
}
}
Basic Model Classes – Java vs Kotlin
public class Movie {
private String title;
private String description;
private Rating rating;
private Genre genre;
public Movie(String title, String description, Rating rating, Genre genre) {
this.title = title;
this.description = description;
this.rating = rating;
this.genre = genre;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Rating getRating() {
return rating;
}
public void setRating(Rating rating) {
this.rating = rating;
}
public Genre getGenre() {
return genre;
}
public void setGenre(Genre genre) {
this.genre = genre;
}
}
data class Movie(var title: String,
var description: String,
var rating: Rating,
var genre: Genre) {
}
Java Kotlin
Basic Model Classes – Java vs Kotlin
import java.util.Objects;
public class Movie {
private String title;
private String description;
private Rating rating;
private Genre genre;
public Movie(String title, String description, Rating rating, Genre genre) {
this.title = title;
this.description = description;
this.rating = rating;
this.genre = genre;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Rating getRating() {
return rating;
}
public void setRating(Rating rating) {
this.rating = rating;
}
public Genre getGenre() {
return genre;
}
public void setGenre(Genre genre) {
this.genre = genre;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Movie movie = (Movie) o;
return Objects.equals(title, movie.title) &&
Objects.equals(description, movie.description) &&
Objects.equals(rating, movie.rating) &&
Objects.equals(genre, movie.genre);
}
@Override
public int hashCode() {
return Objects.hash(title, description, rating, genre);
}
@Override
public String toString() {
return "Movie{" +
"title='" + title + ''' +
", description='" + description + ''' +
", rating=" + rating +
", genre=" + genre +
'}';
}
}
data class Movie(var title: String,
var description: String,
var rating: Rating,
var genre: Genre) {
}
We also get hashCode(), equals(), toString() and more…
External libraries and tools like Lombok will help with Java
Java Kotlin
Null Safety
private fun processUserPolicy(user: User) {
val policy = user.policy
if (policy.isComplete) {
policyProcessor.process(policy)
}
}
private static void processUserPolicy(User user) {
if (user != null && user.getPolicy() != null) {
Policy policy = user.getPolicy();
if (policy.isComplete()) {
policyProcessor.process(policy);
}
}
}
Java
Kotlin
Functional Chaining – vs Java
Reified Generics with a single primitive representation makes the code simpler
double averageSalary = employees
.stream()
.filter(x ->
x.getDepartment() == Engineering)
.mapToInt(Employee::getSalary)
.average()
.orElse(0);
Java
val averageSalary = employees
.filter { it.department === Engineering }
.map { it.salary }
.average()
Kotlin
Kotlin was originally for writing server and desktop Java
• That’s what JetBrains were building themselves
This evolved further into supporting Android
• Which is now what it’s very popular for
JetBrains went further to support the Browser in Kotlin/JS and native in
Kotlin.Native
Kotlin Platforms
Kotlin/Native is a technology for compiling Kotlin code to native binaries
• It runs without a virtual machine
• The output is NOT portable
Although the binary output is not portable, the source IS portable
• The compiler can compile the same source to multiple outputs/platforms
• The source can be placed into a multiplatform project and used for JVM,
JS etc
Kotlin Native
Kotlin Compiler LLVMSource Native Binary
LLVM IR
Supported platforms include,
• iOS (arm32, arm64, emulator x86_64)
• MacOS (x86_64)
• Android (arm32, arm64)
• Windows (mingw x86_64)
• Linux (x86_64, arm32, MIPS, MIPS little endian)
• WebAssembly (wasm32)
We can link to or dynamically call into other native libraries
• C headers or Apple Frameworks
Note – Kotlin/Native is still beta
Kotlin Native
© Instil Software 2018
• Image Convolution
https://en.wikipedia.org/wiki/Kernel_(image_processing)
https://rosettacode.org/wiki/Image_convolution#Kotlin
Demo
Multiplatform project – common and platform specific code
• Using ‘expect’ and ‘actual’
API compatibility shown on doc pages
Demonstration Points – Multiplatform APIs
Common things such as Kotlin.io aren’t available
You can use Posix and interop to achieve functionality or use other multiplatform
libraries
To simplify working with native code we have some methods
• refTo
• pin
• unpin
Demonstration Points – Multiplatform APIs
import kotlinx.cinterop.*
import platform.posix.*
import kotlinx.io.*
Now supports Gradle for building so you can use IntelliJ
For more advanced native debugging you’ll need native debugger e.g. Clion
Demonstration Points - Build
© Instil Software 2018
Summary
Kotlin is spreading
• The right balance between simplicity and productivity
• Easy interoperation with Java
Kotlin/Native is very interesting
• Still beta but JetBrains are working hard
• Provides a solution for iOS support
A good set of libraries allows you to be productive
• Kotlin/Native doesn’t have that…yet
• The interop and wrapping of other libraries will be key
• First class support of more Kotlin libraries will be key
Kotlin, soon to be the right once, run anywhere language
Summary
Well….maybe
Questions?

Kotlin for all the Things

  • 1.
    training@instil.co January 2019 © InstilSoftware 2018 Kotlin for All the Things Kotlin Native @BoyleEamonn Eamonn Boyle
  • 2.
  • 3.
  • 4.
  • 5.
  • 7.
    2010: Work beginson Kotlin within JetBrains 2011: First public announcement on Kotlin 2012: Open sourced under Apache 2 license 2016: Version one released 2017: First class support on Android 2018: Version 1.3 released A Kotlin Timeline
  • 8.
    We’ve bought intoKotlin in a big way Kotlin…Ok…we’re obsessed
  • 9.
    We held aworkshop at the conference • React Web App with Kotlin on Server & Browser Kotlin…Ok…we’re obsessed
  • 10.
    If you’re workingon the Java Virtual Machine, try not to use Java Having done C# for many years I was so happy to see Kotlin for the JVM Problems with Java • Verbose, Verbose, Verbose, Verbose, Verbose, Verbose, Verbose, Verbose, • Nulls • Completely OO Why Koltin - Java – Yuck!
  • 11.
    No need towait - the interop story is so good • Call into all that legacy Java code easily • Make calls into your new Kotlin code from Java easily Really concise, yet clear syntax • Less is more • “Borrows” the best bits of other languages • Less baggage Why Kotlin – So Much to Like Null Safety String Templates Default parameters Extensions Free Functions Coroutines Single Expression Functions Reified generics Data classes and Properties Type Inference Smart Casts Operator overloading
  • 12.
    Basic Model Classes– Java public class Movie { private String title; private String description; private Rating rating; private Genre genre; }
  • 13.
    Basic Model Classes– Java public class Movie { private String title; private String description; private Rating rating; private Genre genre; public Movie(String title, String description, Rating rating, Genre genre) { this.title = title; this.description = description; this.rating = rating; this.genre = genre; } }
  • 14.
    Basic Model Classes– Java public class Movie { private String title; private String description; private Rating rating; private Genre genre; public Movie(String title, String description, Rating rating, Genre genre) { this.title = title; this.description = description; this.rating = rating; this.genre = genre; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; }
  • 15.
    Basic Model Classes– Java public class Movie { private String title; private String description; private Rating rating; private Genre genre; public Movie(String title, String description, Rating rating, Genre genre) { this.title = title; this.description = description; this.rating = rating; this.genre = genre; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public Rating getRating() { return rating; } public void setRating(Rating rating) { this.rating = rating; } public Genre getGenre() { return genre; } public void setGenre(Genre genre) { this.genre = genre; } }
  • 16.
    Basic Model Classes– Java vs Kotlin public class Movie { private String title; private String description; private Rating rating; private Genre genre; public Movie(String title, String description, Rating rating, Genre genre) { this.title = title; this.description = description; this.rating = rating; this.genre = genre; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public Rating getRating() { return rating; } public void setRating(Rating rating) { this.rating = rating; } public Genre getGenre() { return genre; } public void setGenre(Genre genre) { this.genre = genre; } } data class Movie(var title: String, var description: String, var rating: Rating, var genre: Genre) { } Java Kotlin
  • 17.
    Basic Model Classes– Java vs Kotlin import java.util.Objects; public class Movie { private String title; private String description; private Rating rating; private Genre genre; public Movie(String title, String description, Rating rating, Genre genre) { this.title = title; this.description = description; this.rating = rating; this.genre = genre; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public Rating getRating() { return rating; } public void setRating(Rating rating) { this.rating = rating; } public Genre getGenre() { return genre; } public void setGenre(Genre genre) { this.genre = genre; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Movie movie = (Movie) o; return Objects.equals(title, movie.title) && Objects.equals(description, movie.description) && Objects.equals(rating, movie.rating) && Objects.equals(genre, movie.genre); } @Override public int hashCode() { return Objects.hash(title, description, rating, genre); } @Override public String toString() { return "Movie{" + "title='" + title + ''' + ", description='" + description + ''' + ", rating=" + rating + ", genre=" + genre + '}'; } } data class Movie(var title: String, var description: String, var rating: Rating, var genre: Genre) { } We also get hashCode(), equals(), toString() and more… External libraries and tools like Lombok will help with Java Java Kotlin
  • 19.
    Null Safety private funprocessUserPolicy(user: User) { val policy = user.policy if (policy.isComplete) { policyProcessor.process(policy) } } private static void processUserPolicy(User user) { if (user != null && user.getPolicy() != null) { Policy policy = user.getPolicy(); if (policy.isComplete()) { policyProcessor.process(policy); } } } Java Kotlin
  • 20.
    Functional Chaining –vs Java Reified Generics with a single primitive representation makes the code simpler double averageSalary = employees .stream() .filter(x -> x.getDepartment() == Engineering) .mapToInt(Employee::getSalary) .average() .orElse(0); Java val averageSalary = employees .filter { it.department === Engineering } .map { it.salary } .average() Kotlin
  • 21.
    Kotlin was originallyfor writing server and desktop Java • That’s what JetBrains were building themselves This evolved further into supporting Android • Which is now what it’s very popular for JetBrains went further to support the Browser in Kotlin/JS and native in Kotlin.Native Kotlin Platforms
  • 22.
    Kotlin/Native is atechnology for compiling Kotlin code to native binaries • It runs without a virtual machine • The output is NOT portable Although the binary output is not portable, the source IS portable • The compiler can compile the same source to multiple outputs/platforms • The source can be placed into a multiplatform project and used for JVM, JS etc Kotlin Native Kotlin Compiler LLVMSource Native Binary LLVM IR
  • 23.
    Supported platforms include, •iOS (arm32, arm64, emulator x86_64) • MacOS (x86_64) • Android (arm32, arm64) • Windows (mingw x86_64) • Linux (x86_64, arm32, MIPS, MIPS little endian) • WebAssembly (wasm32) We can link to or dynamically call into other native libraries • C headers or Apple Frameworks Note – Kotlin/Native is still beta Kotlin Native
  • 24.
    © Instil Software2018 • Image Convolution https://en.wikipedia.org/wiki/Kernel_(image_processing) https://rosettacode.org/wiki/Image_convolution#Kotlin Demo
  • 25.
    Multiplatform project –common and platform specific code • Using ‘expect’ and ‘actual’ API compatibility shown on doc pages Demonstration Points – Multiplatform APIs
  • 26.
    Common things suchas Kotlin.io aren’t available You can use Posix and interop to achieve functionality or use other multiplatform libraries To simplify working with native code we have some methods • refTo • pin • unpin Demonstration Points – Multiplatform APIs import kotlinx.cinterop.* import platform.posix.* import kotlinx.io.*
  • 27.
    Now supports Gradlefor building so you can use IntelliJ For more advanced native debugging you’ll need native debugger e.g. Clion Demonstration Points - Build
  • 28.
    © Instil Software2018 Summary
  • 29.
    Kotlin is spreading •The right balance between simplicity and productivity • Easy interoperation with Java Kotlin/Native is very interesting • Still beta but JetBrains are working hard • Provides a solution for iOS support A good set of libraries allows you to be productive • Kotlin/Native doesn’t have that…yet • The interop and wrapping of other libraries will be key • First class support of more Kotlin libraries will be key Kotlin, soon to be the right once, run anywhere language Summary Well….maybe
  • 30.