SlideShare a Scribd company logo
training@instil.co
ACCU - November 2019
© Instil Software 2019
Interop Between Kotlin Native and C / Swift
The Good, the Bad and the Ugly @BoyleEamonn
Eamonn Boyle
@GarthGilmour
Garth Gilmour
Me
Him
Us
Google Maps
Kotlin Native - C / Swift Interop - ACCU Autmn 2019
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 App with Kotlin on Server & Browser
Kotlin…Ok…we’re obsessed
We’re going back again…
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 Kotlin - Java – Yuck!
No need to wait - the interop story is so good
• Call into all that legacy Java code easily
• Call 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
Kotlin Native - C / Swift Interop - ACCU Autmn 2019
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, Raspberry Pi)
• WebAssembly (wasm32)
We can link to or dynamically call into other native libraries
• C, Swift or ObjectiveC libraries
Note – Kotlin/Native is still beta
Kotlin Native
Scenarios
Kotlin Native
Executable
Kotlin Native
Library
Platform Native
Executable
Kotlin Native
Executable
Platform Native
Library
Multiplatform Project
Executable or Library
Multiple Targets in One Project
Common Code
Platform Specific Code
iOS Android
You can drive everything from the command line
• Kotlin Native CLI tools
• Gradle for fully builder system
An IDE can make the whole experience much easier
IDEs
Common JVM APIs such as kotlin.io aren’t available
But there are multi-platform and native libraries that you can consume
Kotlin Native also supports excellent interop with C and ObjectiveC/Swift
API compatibility shown on doc pages
Supported APIs
import kotlinx.io.*
import kotlinx.cinterop.*
To simplify things further, common native APIs have already been wrapped
These bindings are platform specific but include
• Posix
• zlib
• GDI (Windows)
• OpenGL
• Apple Frameworks
Platform Libraries
import platform.posix.*
A major reason for choosing Kotlin is that it is a great language
For Kotlin Native, the language remains unchanged
• Though the implementation and runtime is wildly different
In native, it uses reference counting for automated memory management
• And an additional cyclical garbage collector
Advantages of the Kotlin Language
Let’s have a look at native features and along the
way see some nice features of the language
generally
Advantages of the Kotlin Language
© Instil Software 2019
• Kotlin Native Examples
Demo
Scenarios
Kotlin Native
Executable
Dynamic Library
C & C++
Apple
Framework
Swift
Bitwise Operations
Data can be copied back and forth between Kotlin and C
• Kotlin Native can automatically marshal from C primitive types
Primitive Types
C Kotlin
char kotlin.Byte
unsigned char kotlin.UByte
short kotlin.Short
unsigned short kotlin.UShort
int kotlin.Int
unsigned int kotlin.UInt
long long kotlin.Long
unsigned long long kotlin.ULong
float kotlin.Float
double kotlin.Double
If directly referencing C data, Kotlin Native uses a CVariable type
Internally, it contains a reference to C data via a NativePtr type
• You can think of it a little like a wrapper or C++ reference or lvalue
CVariable
CVariable
NativePtr
0x02677dd0
Kotlin Native
0x0000000002677d00 00000000 00000000 581cbfec 7a7b0430 │ ········X···z{·0 │
0x0000000002677d10 107d6702 00000000 587d6702 00000000 │ ·}g·····X}g····· │
0x0000000002677d20 abababab abababab abababab abababab │ ················ │
0x0000000002677d30 00000000 00000000 00000000 00000000 │ ················ │
0x0000000002677d40 eefeeefe eefeeefe 581cbfec 7b7b0430 │ ········X···{{·0 │
0x0000000002677d50 0d000000 04080000 30aa4700 00000000 │ ········0·G····· │
0x0000000002677d60 abababab abababab abababab abababab │ ················ │
0x0000000002677d70 00000000 00000000 00000000 00000000 │ ················ │
0x0000000002677d80 00000000 00000000 581cbfec 7b7b0437 │ ········X···{{·7 │
0x0000000002677d90 00000000 00000000 00ababab abababab │ ················ │
0x0000000002677da0 abababab abababab ab000000 00000000 │ ················ │
0x0000000002677db0 00000000 00000000 00000000 00000000 │ ················ │
0x0000000002677dc0 00000000 00000000 7c1cbccb 7b7b0400 │ ········|···{{·· │
0x0000000002677dd0 b05f0201 00000000 e0200201 00000000 │ ·_······· ······ │
0x0000000002677de0 eefeeefe eefeeefe eefeeefe eefeeefe │ ················ │
0x0000000002677df0 eefeeefe eefeeefe eefeeefe eefeeefe │ ················ │
There are CVariable types for,
• All the primitives - IntVar, DoubleVar etc
• Enums – CEnumVar<T>
• Structs – CStructVar<T>
• Pointers (more on this later) – CPointerVar<T>
CVariables allow reading and writing via the value property
CVariable Types
fun sum(total: IntVar, current: Int) {
total.value += current
}
CVariable Types
CPointed
NativePointed
CEnumVar
BooleanVar
ByteVar
ShortVar
IntVar
LongVar
FloatVar
DoubleVar
. . .
CStructVar CPrimitiveVarCPointerVar
CVariable CFunction
Structs can be easily created in C and mapped to Kotlin
• The mapping is done automatically by including headers in the def file
Structs
class Employee(rawPtr: NativePtr) : CStructVar(rawPtr) {
companion object : Type(24, 8)
var name: CPointer<ByteVar>?
get() = memberAt<CPointerVar<ByteVar>>(0).value
set(value) { memberAt<CPointerVar<ByteVar>>(0).value = value }
var age: Int
get() = memberAt<IntVar>(8).value
set(value) { memberAt<IntVar>(8).value = value }
var salary: Double
get() = memberAt<DoubleVar>(16).value
set(value) { memberAt<DoubleVar>(16).value = value }
}
typedef struct {
char* name;
int age;
double salary;
} Employee;
The type name matches the C typedef but it is a CStructVar
Structs – Points to Note
class Employee(rawPtr: NativePtr) : CStructVar(rawPtr) {
companion object : Type(24, 8)
...
}
typedef struct {
char* name;
int age;
double salary;
} Employee;
Internally it uses addresses to get CVariables but marshals into Kotlin types
Structs – Points to Note
class Employee(rawPtr: NativePtr) : CStructVar(rawPtr) {
...
var age: Int
get() = memberAt<IntVar>(8).value
set(value) { memberAt<IntVar>(8).value = value }
var salary: Double
get() = memberAt<DoubleVar>(16).value
set(value) { memberAt<DoubleVar>(16).value = value }
} typedef struct {
char* name;
int age;
double salary;
} Employee;
Except where the returned type is a C type
• E.g. pointers
Structs – Points to Note
class Employee(rawPtr: NativePtr) : CStructVar(rawPtr) {
var name: CPointer<ByteVar>?
get() = memberAt<CPointerVar<ByteVar>>(0).value
set(value) { memberAt<CPointerVar<ByteVar>>(0).value = value }
...
}
typedef struct {
char* name;
int age;
double salary;
} Employee;
You will note that structs have a Type that includes size and alignment info
This facilitates allocation
Allocating Structs
class Employee(rawPtr: NativePtr)
: CStructVar(rawPtr) {
companion object : Type(24, 8)
...
}
typedef struct {
char* name;
int age;
double salary;
} Employee;
Pointers
https://xkcd.com/138/
As well as CVariables, Kotlin Native supports pointers via CPointer<T>
The type of data pointed to, T, must be a CVariable type
• It’s actually CPointed which can be a CFunction, but more on that later
Pointers
Kotlin Native
0x0000000002677d00 00000000 00000000 581cbfec 7a7b0430 │ ········X···z{·0 │
0x0000000002677d10 107d6702 00000000 587d6702 00000000 │ ·}g·····X}g····· │
0x0000000002677d20 abababab abababab abababab abababab │ ················ │
0x0000000002677d30 00000000 00000000 00000000 00000000 │ ················ │
0x0000000002677d40 eefeeefe eefeeefe 581cbfec 7b7b0430 │ ········X···{{·0 │
0x0000000002677d50 0d000000 04080000 30aa4700 00000000 │ ········0·G····· │
0x0000000002677d60 abababab abababab abababab abababab │ ················ │
0x0000000002677d70 00000000 00000000 00000000 00000000 │ ················ │
0x0000000002677d80 00000000 00000000 581cbfec 7b7b0437 │ ········X···{{·7 │
0x0000000002677d90 00000000 00000000 00ababab abababab │ ················ │
0x0000000002677da0 abababab abababab ab000000 00000000 │ ················ │
0x0000000002677db0 00000000 00000000 00000000 00000000 │ ················ │
0x0000000002677dc0 00000000 00000000 7c1cbccb 7b7b0400 │ ········|···{{·· │
0x0000000002677dd0 b05f0201 00000000 e0200201 00000000 │ ·_······· ······ │
0x0000000002677de0 eefeeefe eefeeefe eefeeefe eefeeefe │ ················ │
0x0000000002677df0 eefeeefe eefeeefe eefeeefe eefeeefe │ ················ │
CPointer<T>
NativePtr
0x02677dd0
T = IntVar
The difference in CPointer and CVariable is subtle
• They both contain an address/reference to some data in memory
• The difference is similar to that between C++ references & C++ pointer
A CVariable facilitates reading and writing of typed data
• Without considering pointers
A CPointer exposes the concept of a pointer for programmatic use
• Passing around, dereferencing, operations etc.
CPointer & CVariable
Pointer parameters of C Functions are mapped to CValuesRef<T>
CValuesRef
fun sum(numbers: CValuesRef<IntVar>?, size: Int): Int
int sum(const int * numbers, int size)
This is designed to make passing Kotlin data to functions easier
We can create temporary copies of the data to be used only for the call
The data is copied to and lives in Kotlin space
• This copying can be very expensive
A CPointer extends from a CValuesRef so it can be passed in directly
CValuesRef
CPointer<T>
CValuesRef<T>
CValues<T>
CValuesRef
Kotlin Native
0x0000000002677d00 00000000 00000000 581cbfec 7a7b0430 │ ········X···z{·0 │
0x0000000002677d10 107d6702 00000000 587d6702 00000000 │ ·}g·····X}g····· │
0x0000000002677d20 abababab abababab abababab abababab │ ················ │
0x0000000002677d30 00000000 00000000 00000000 00000000 │ ················ │
0x0000000002677d40 eefeeefe eefeeefe 581cbfec 7b7b0430 │ ········X···{{·0 │
0x0000000002677d50 0d000000 04080000 30aa4700 00000000 │ ········0·G····· │
0x0000000002677d60 abababab abababab abababab abababab │ ················ │
0x0000000002677d70 00000000 00000000 00000000 00000000 │ ················ │
0x0000000002677d80 00000000 00000000 581cbfec 7b7b0437 │ ········X···{{·7 │
0x0000000002677d90 00000000 00000000 00ababab abababab │ ················ │
0x0000000002677da0 abababab abababab ab000000 00000000 │ ················ │
0x0000000002677db0 00000000 00000000 00000000 00000000 │ ················ │
0x0000000002677dc0 00000000 00000000 7c1cbccb 7b7b0400 │ ········|···{{·· │
0x0000000002677dd0 b05f0201 00000000 e0200201 00000000 │ ·_······· ······ │
0x0000000002677de0 eefeeefe eefeeefe eefeeefe eefeeefe │ ················ │
0x0000000002677df0 eefeeefe eefeeefe eefeeefe eefeeefe │ ················ │
CPointer<T>
NativePtr
0x02677dd0
T = IntVar
CValue<T>
ByteArray at 0x0ff75012
eefeeefe eefeeefe 581cbfec 7b7b0430
0d000000 04080000 30aa4700 00000000
abababab abababab abababab abababab
...
A Kotlin function’s C type is a CFunction
It can be obtained by calling staticCFunction
It only works with unbound functions
• So instance method references will not work (as they capture the object)
Lambda and static function references are both supported
Passing Kotlin Functions into C
CPointed
NativePointed
CVariable CFunction
© Instil Software 2019
Multi-Threading
To avoid common pitfalls of multi-threading with shared mutable state,
Kotlin imposes restrictions on how data can be shared between threads
Only one thread may own mutable data
• Ownership can be transferred
Only immutable data or ‘frozen’ data can be shared between threads
• Any writes to frozen data results in an ‘InvalidMutabilityException’
Constraints in Sharing Data
Shared Mutable
XOR
⊕
Only Frozen or Immutable data can be safely shared between threads
Singleton objects and enums are automatically frozen
Arbitrary objects can be frozen via the ‘freeze’ method
Global variables can be marked with @SharedImmutable
Global variables can also be marked with @ThreadLocal
• In which case each thread gets its own copy of the data
Freezing
These constraints apply to Kotlin data
• Natively allocated data will not have these restrictions imposed
There are also atomic constructs which can be mutated
• AtomicInt
• AtomicDouble
• Etc
Obviously, if using these constructs then you have all the potential pitfalls
• Race conditions, deadlocks etc
Breaking the Rules
© Instil Software 2019
Coroutines
A good ecosystem is essential for rapid development
As a lot of Kotlin code is multi-platform by default, many libraries can be
consumed by Kotlin Native
• Sometimes only partially support
A good example of this is the coroutines library
Using Libraries
“Coroutines are computer-program components
that generalize subroutines for non-preemptive
multitasking, by allowing multiple entry points
for suspending and resuming execution at
certain locations.”
Wikipedia
Coroutine
1 1 2 2 3 3 4 4 5 5 6 6 7 7
1
1
2
2
3
3
4
4
5
5
6
6
7
7
Consider 3 independent jobs of work
Event Loop with Asynchronous Blocks
Start 5 6EndBlocking Start 5 6EndBlocking Start 5 6EndBlocking
Start 5 6EndBlocking
Start 5 6EndBlocking
Start 5 6EndBlocking
If we want to run these in parallel we could create multiple threads
Asynchronous Use Case – Thread Pools
Start End
Blocking
Start End
Blocking
Start 5 6End
Blocking
Coroutines as Light Weight Threads
Start EndBlocking
Start EndBlocking
Start 5 6EndBlocking
Coroutines Scale
Start EndBlocking
Start EndBlocking
Start 5 6EndBlocking
Start EndBlocking
Start EndBlocking
Start 5 6EndBlocking
Start EndBlocking
© Instil Software 2019
• Objective C Interop
Swift Interop
Swift itself is not supported but Objective C is supported
Swift APIs can be exported to Objective C by annotating with @objc
Bidirectional interop with Objective C is more intuitive & complete than C
• Classes and interfaces/protocols can be used
• Reference counting and garbage collection are integrated
• More types are automatically mapped e.g. List
Objective C & Swift Interop
We will focus on the interop story with Swift rather than Objective C directly
• It is a more modern language and the obvious choice for new development
Since Swift is translated to Objective C, we immediately lose features
• Generics
• Tuples
• Enumerations defined in Swift without Int raw value type
• Structures defined in Swift
• Top-level functions and global variables defined in Swift
• Typealiases defined in Swift
• Swift-style variadics
• Nested types
• Curried functions
Objective C & Swift Interop
We can also compile Kotlin into Apple frameworks and libraries
Under this scenario we do not have support for,
• Suspend functions
• Inline classes
But many things map intuitively
Objective C & Swift Interop
Note that for elements to get translated, they must be,
• Annotated with @objc
• Public within a public class
Translations
@objc
public class Employee : NSObject {
@objc public let name: String
@objc public var age: Int
@objc public var salary: Double
public let noObjC = ""
@objc let notPublic = ""
open class Employee : NSObject {
@ObjCMethod("name", "@16@0:8")
external open fun name(): String
@ObjCMethod("age", "q16@0:8")
external open fun age(): NSInteger
@ObjCMethod("setAge:", "v24@0:8q16")
external open fun setAge(age: NSInteger): Unit
@ObjCMethod("salary", "d16@0:8")
external open fun salary(): Double
@ObjCMethod("setSalary:", "v24@0:8d16")
external open fun setSalary(salary: Double): Unit
// ...
Mappings
Kotlin Swift Objective-C
class class @interface
interface protocol @protocol
constructor/create Initializer Initializer
Property Property Property
Method Method Method
@Throws throws error:(NSError**)error
Extension Extension Category member
companion member <- Class method or property Class method or property
null nil nil
Singleton Singleton() [Singleton singleton]
Kotlin Swift Objective-C
Primitive type Primitive type / NSNumber
Unit return type Void void
String String NSString
String NSMutableString NSMutableString
List Array NSArray
MutableList NSMutableArray NSMutableArray
Set Set NSSet
MutableSet NSMutableSet NSMutableSet
Map Dictionary NSDictionary
MutableMap NSMutableDictionary NSMutableDictionary
Function type Function type Block pointer type
Mapping
Function types also have good support including closures
Note that primitive parameters will be boxed
Type differences may cause function reference issues
• E.g. Expecting an a Long parameter for an Int parameter
Function Types
@objc
public func filter(numbers: [Int], predicate: (Int) -> Bool) -> [Int]
external open fun filter(numbers: List<*>,
predicate: (NSInteger) -> Boolean): List<*>
Swift
Kotlin
© Instil Software 2019
Summary
C++ interop is not there, only via C
Swift interop is good but not fully there
• Since it is translating via Objective C
Build times are slow - especially when compared with modern languages
• E.g. Kotlin/Java on JVM, TypeScript, C#
The Bad
Still beta so there are some issues
• In Windows the IDE keeps locking the klib so I need to restart
• Autocomplete not in CLion for Gradle KTS
• Documentation incomplete or out of date
Encountered memory leak that I couldn’t quite track down
The Bad
With C, you will need to switch between Strings and pointers to Byte
• It will depend on the usage case
• E.g. function calls, struct fields, function pointers
You miss some of the features of the Java Standard Library
• Posix give some cross platform features but …
• There are differences in the APIs across platforms
• They are not Kotlin first so the usage is clunky
Performance is not there
• Generally when compared with say a JVM implementation
• CValues can have severe performance impact
The Bad
The Kotlin language is excellent so using in more environments is good
The tooling around code authoring is excellent
• Though documentation and debugging is not there yet
Interop with C and Objective C is fairly good
• Especially Swift/Objective C with memory management, closures etc.
The potential for sharing code across platforms is appealing
The Good
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
Summary
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 write once, run anywhere language
Summary
Well….maybe
Questions?

More Related Content

PPT
History of the theatre lesson 4 modern theatre
PPTX
The literary term catharsis
PPTX
Theatre stages and_terms
PPT
Chapter 6: The Playwright and the Script
PPTX
Modern theater
PPTX
The rape of the lock
PPT
Scenic Design Chapter 15
PPT
Types of stages and drapes - Theatre 1
History of the theatre lesson 4 modern theatre
The literary term catharsis
Theatre stages and_terms
Chapter 6: The Playwright and the Script
Modern theater
The rape of the lock
Scenic Design Chapter 15
Types of stages and drapes - Theatre 1

What's hot (20)

PPT
History of theatre
PPTX
similarities and Differences Between Neoclassicism and Romanticism
PPTX
PPT on Victorian novelists
PPTX
Literary Movements in English Literature - Part I
PPT
Native American Lit
PPT
Week 4 Plato And Art
PPTX
What is performance
PPT
Donne ppt
PPS
Nature of theatre and ritual
PPTX
Modernist Literature
PPT
Chapter 1: Theatre, Art, and Entertainment
PPT
Elements of drama
PPTX
Oscar Wilde
PPTX
Restoration presentation
PPTX
History of drama
PPTX
Plato AND HIS ART & LITERATURE
DOCX
Roman Theatre
DOCX
Revised shakespeare preface to
PDF
What Is Literary Criticism
PPTX
Medieval drama
History of theatre
similarities and Differences Between Neoclassicism and Romanticism
PPT on Victorian novelists
Literary Movements in English Literature - Part I
Native American Lit
Week 4 Plato And Art
What is performance
Donne ppt
Nature of theatre and ritual
Modernist Literature
Chapter 1: Theatre, Art, and Entertainment
Elements of drama
Oscar Wilde
Restoration presentation
History of drama
Plato AND HIS ART & LITERATURE
Roman Theatre
Revised shakespeare preface to
What Is Literary Criticism
Medieval drama
Ad

Similar to Kotlin Native - C / Swift Interop - ACCU Autmn 2019 (20)

PPTX
Kotlin for all the Things
PPTX
Kotlin for Android - Goto Copenhagan 2019
PDF
Transitioning Android Teams Into Kotlin
PDF
Kotlin for Android Developers - 1
PDF
Is this Swift for Android? A short introduction to the Kotlin language
PPTX
Kotlin - A Programming Language
PPTX
Introduction to Kotlin Language and its application to Android platform
PDF
A short introduction to the Kotlin language for Java developers
PDF
Programming with Kotlin
PPTX
Kotlin – the future of android
PDF
JDD 2017: Kotlin for Java developers (Tomasz Kleszczyński)
PPTX
Introduction to Kotlin
PDF
Kotlin, smarter development for the jvm
PDF
Little Helpers for Android Development with Kotlin
PDF
Kotlin: a better Java
PDF
Android 101 - Kotlin ( Future of Android Development)
PDF
Kotlin from-scratch
PPTX
PPTX
Kotlin
Kotlin for all the Things
Kotlin for Android - Goto Copenhagan 2019
Transitioning Android Teams Into Kotlin
Kotlin for Android Developers - 1
Is this Swift for Android? A short introduction to the Kotlin language
Kotlin - A Programming Language
Introduction to Kotlin Language and its application to Android platform
A short introduction to the Kotlin language for Java developers
Programming with Kotlin
Kotlin – the future of android
JDD 2017: Kotlin for Java developers (Tomasz Kleszczyński)
Introduction to Kotlin
Kotlin, smarter development for the jvm
Little Helpers for Android Development with Kotlin
Kotlin: a better Java
Android 101 - Kotlin ( Future of Android Development)
Kotlin from-scratch
Kotlin
Ad

More from Eamonn Boyle (6)

PPTX
2019-06 - Goto Amsterdam - Microservices
PPTX
2019-01-29 - Demystifying Kotlin Coroutines
PPTX
BelTech 2017 - Building Quality in the Browser
PPTX
ASP .Net Core SPA Templates
PPTX
Being Expressive in Code
PPTX
2018-09 - F# and Fable
2019-06 - Goto Amsterdam - Microservices
2019-01-29 - Demystifying Kotlin Coroutines
BelTech 2017 - Building Quality in the Browser
ASP .Net Core SPA Templates
Being Expressive in Code
2018-09 - F# and Fable

Recently uploaded (20)

PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PPTX
Transform Your Business with a Software ERP System
PDF
System and Network Administraation Chapter 3
PPTX
Online Work Permit System for Fast Permit Processing
PPTX
history of c programming in notes for students .pptx
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PDF
Softaken Excel to vCard Converter Software.pdf
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PPTX
ManageIQ - Sprint 268 Review - Slide Deck
PPTX
Operating system designcfffgfgggggggvggggggggg
PPTX
Introduction to Artificial Intelligence
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PDF
Understanding Forklifts - TECH EHS Solution
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
VVF-Customer-Presentation2025-Ver1.9.pptx
Transform Your Business with a Software ERP System
System and Network Administraation Chapter 3
Online Work Permit System for Fast Permit Processing
history of c programming in notes for students .pptx
Upgrade and Innovation Strategies for SAP ERP Customers
Softaken Excel to vCard Converter Software.pdf
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
ManageIQ - Sprint 268 Review - Slide Deck
Operating system designcfffgfgggggggvggggggggg
Introduction to Artificial Intelligence
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
Odoo Companies in India – Driving Business Transformation.pdf
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
Understanding Forklifts - TECH EHS Solution

Kotlin Native - C / Swift Interop - ACCU Autmn 2019

  • 1. training@instil.co ACCU - November 2019 © Instil Software 2019 Interop Between Kotlin Native and C / Swift The Good, the Bad and the Ugly @BoyleEamonn Eamonn Boyle @GarthGilmour Garth Gilmour
  • 2. Me
  • 3. Him
  • 4. Us
  • 7. 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
  • 8. We’ve bought into Kotlin in a big way Kotlin…Ok…we’re obsessed
  • 9. We held a workshop at the conference • React App with Kotlin on Server & Browser Kotlin…Ok…we’re obsessed
  • 10. We’re going back again…
  • 11. 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 Kotlin - Java – Yuck!
  • 12. No need to wait - the interop story is so good • Call into all that legacy Java code easily • Call 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
  • 13. Basic Model Classes – Java public class Movie { private String title; private String description; private Rating rating; private 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; } }
  • 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; }
  • 16. 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; } }
  • 17. 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
  • 18. 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
  • 20. 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
  • 21. 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
  • 22. 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
  • 23. 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
  • 24. 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, Raspberry Pi) • WebAssembly (wasm32) We can link to or dynamically call into other native libraries • C, Swift or ObjectiveC libraries Note – Kotlin/Native is still beta Kotlin Native
  • 25. Scenarios Kotlin Native Executable Kotlin Native Library Platform Native Executable Kotlin Native Executable Platform Native Library Multiplatform Project Executable or Library Multiple Targets in One Project Common Code Platform Specific Code iOS Android
  • 26. You can drive everything from the command line • Kotlin Native CLI tools • Gradle for fully builder system An IDE can make the whole experience much easier IDEs
  • 27. Common JVM APIs such as kotlin.io aren’t available But there are multi-platform and native libraries that you can consume Kotlin Native also supports excellent interop with C and ObjectiveC/Swift API compatibility shown on doc pages Supported APIs import kotlinx.io.* import kotlinx.cinterop.*
  • 28. To simplify things further, common native APIs have already been wrapped These bindings are platform specific but include • Posix • zlib • GDI (Windows) • OpenGL • Apple Frameworks Platform Libraries import platform.posix.*
  • 29. A major reason for choosing Kotlin is that it is a great language For Kotlin Native, the language remains unchanged • Though the implementation and runtime is wildly different In native, it uses reference counting for automated memory management • And an additional cyclical garbage collector Advantages of the Kotlin Language
  • 30. Let’s have a look at native features and along the way see some nice features of the language generally Advantages of the Kotlin Language
  • 31. © Instil Software 2019 • Kotlin Native Examples Demo
  • 34. Data can be copied back and forth between Kotlin and C • Kotlin Native can automatically marshal from C primitive types Primitive Types C Kotlin char kotlin.Byte unsigned char kotlin.UByte short kotlin.Short unsigned short kotlin.UShort int kotlin.Int unsigned int kotlin.UInt long long kotlin.Long unsigned long long kotlin.ULong float kotlin.Float double kotlin.Double
  • 35. If directly referencing C data, Kotlin Native uses a CVariable type Internally, it contains a reference to C data via a NativePtr type • You can think of it a little like a wrapper or C++ reference or lvalue CVariable CVariable NativePtr 0x02677dd0 Kotlin Native 0x0000000002677d00 00000000 00000000 581cbfec 7a7b0430 │ ········X···z{·0 │ 0x0000000002677d10 107d6702 00000000 587d6702 00000000 │ ·}g·····X}g····· │ 0x0000000002677d20 abababab abababab abababab abababab │ ················ │ 0x0000000002677d30 00000000 00000000 00000000 00000000 │ ················ │ 0x0000000002677d40 eefeeefe eefeeefe 581cbfec 7b7b0430 │ ········X···{{·0 │ 0x0000000002677d50 0d000000 04080000 30aa4700 00000000 │ ········0·G····· │ 0x0000000002677d60 abababab abababab abababab abababab │ ················ │ 0x0000000002677d70 00000000 00000000 00000000 00000000 │ ················ │ 0x0000000002677d80 00000000 00000000 581cbfec 7b7b0437 │ ········X···{{·7 │ 0x0000000002677d90 00000000 00000000 00ababab abababab │ ················ │ 0x0000000002677da0 abababab abababab ab000000 00000000 │ ················ │ 0x0000000002677db0 00000000 00000000 00000000 00000000 │ ················ │ 0x0000000002677dc0 00000000 00000000 7c1cbccb 7b7b0400 │ ········|···{{·· │ 0x0000000002677dd0 b05f0201 00000000 e0200201 00000000 │ ·_······· ······ │ 0x0000000002677de0 eefeeefe eefeeefe eefeeefe eefeeefe │ ················ │ 0x0000000002677df0 eefeeefe eefeeefe eefeeefe eefeeefe │ ················ │
  • 36. There are CVariable types for, • All the primitives - IntVar, DoubleVar etc • Enums – CEnumVar<T> • Structs – CStructVar<T> • Pointers (more on this later) – CPointerVar<T> CVariables allow reading and writing via the value property CVariable Types fun sum(total: IntVar, current: Int) { total.value += current }
  • 38. Structs can be easily created in C and mapped to Kotlin • The mapping is done automatically by including headers in the def file Structs class Employee(rawPtr: NativePtr) : CStructVar(rawPtr) { companion object : Type(24, 8) var name: CPointer<ByteVar>? get() = memberAt<CPointerVar<ByteVar>>(0).value set(value) { memberAt<CPointerVar<ByteVar>>(0).value = value } var age: Int get() = memberAt<IntVar>(8).value set(value) { memberAt<IntVar>(8).value = value } var salary: Double get() = memberAt<DoubleVar>(16).value set(value) { memberAt<DoubleVar>(16).value = value } } typedef struct { char* name; int age; double salary; } Employee;
  • 39. The type name matches the C typedef but it is a CStructVar Structs – Points to Note class Employee(rawPtr: NativePtr) : CStructVar(rawPtr) { companion object : Type(24, 8) ... } typedef struct { char* name; int age; double salary; } Employee;
  • 40. Internally it uses addresses to get CVariables but marshals into Kotlin types Structs – Points to Note class Employee(rawPtr: NativePtr) : CStructVar(rawPtr) { ... var age: Int get() = memberAt<IntVar>(8).value set(value) { memberAt<IntVar>(8).value = value } var salary: Double get() = memberAt<DoubleVar>(16).value set(value) { memberAt<DoubleVar>(16).value = value } } typedef struct { char* name; int age; double salary; } Employee;
  • 41. Except where the returned type is a C type • E.g. pointers Structs – Points to Note class Employee(rawPtr: NativePtr) : CStructVar(rawPtr) { var name: CPointer<ByteVar>? get() = memberAt<CPointerVar<ByteVar>>(0).value set(value) { memberAt<CPointerVar<ByteVar>>(0).value = value } ... } typedef struct { char* name; int age; double salary; } Employee;
  • 42. You will note that structs have a Type that includes size and alignment info This facilitates allocation Allocating Structs class Employee(rawPtr: NativePtr) : CStructVar(rawPtr) { companion object : Type(24, 8) ... } typedef struct { char* name; int age; double salary; } Employee;
  • 44. As well as CVariables, Kotlin Native supports pointers via CPointer<T> The type of data pointed to, T, must be a CVariable type • It’s actually CPointed which can be a CFunction, but more on that later Pointers Kotlin Native 0x0000000002677d00 00000000 00000000 581cbfec 7a7b0430 │ ········X···z{·0 │ 0x0000000002677d10 107d6702 00000000 587d6702 00000000 │ ·}g·····X}g····· │ 0x0000000002677d20 abababab abababab abababab abababab │ ················ │ 0x0000000002677d30 00000000 00000000 00000000 00000000 │ ················ │ 0x0000000002677d40 eefeeefe eefeeefe 581cbfec 7b7b0430 │ ········X···{{·0 │ 0x0000000002677d50 0d000000 04080000 30aa4700 00000000 │ ········0·G····· │ 0x0000000002677d60 abababab abababab abababab abababab │ ················ │ 0x0000000002677d70 00000000 00000000 00000000 00000000 │ ················ │ 0x0000000002677d80 00000000 00000000 581cbfec 7b7b0437 │ ········X···{{·7 │ 0x0000000002677d90 00000000 00000000 00ababab abababab │ ················ │ 0x0000000002677da0 abababab abababab ab000000 00000000 │ ················ │ 0x0000000002677db0 00000000 00000000 00000000 00000000 │ ················ │ 0x0000000002677dc0 00000000 00000000 7c1cbccb 7b7b0400 │ ········|···{{·· │ 0x0000000002677dd0 b05f0201 00000000 e0200201 00000000 │ ·_······· ······ │ 0x0000000002677de0 eefeeefe eefeeefe eefeeefe eefeeefe │ ················ │ 0x0000000002677df0 eefeeefe eefeeefe eefeeefe eefeeefe │ ················ │ CPointer<T> NativePtr 0x02677dd0 T = IntVar
  • 45. The difference in CPointer and CVariable is subtle • They both contain an address/reference to some data in memory • The difference is similar to that between C++ references & C++ pointer A CVariable facilitates reading and writing of typed data • Without considering pointers A CPointer exposes the concept of a pointer for programmatic use • Passing around, dereferencing, operations etc. CPointer & CVariable
  • 46. Pointer parameters of C Functions are mapped to CValuesRef<T> CValuesRef fun sum(numbers: CValuesRef<IntVar>?, size: Int): Int int sum(const int * numbers, int size)
  • 47. This is designed to make passing Kotlin data to functions easier We can create temporary copies of the data to be used only for the call The data is copied to and lives in Kotlin space • This copying can be very expensive A CPointer extends from a CValuesRef so it can be passed in directly CValuesRef CPointer<T> CValuesRef<T> CValues<T>
  • 48. CValuesRef Kotlin Native 0x0000000002677d00 00000000 00000000 581cbfec 7a7b0430 │ ········X···z{·0 │ 0x0000000002677d10 107d6702 00000000 587d6702 00000000 │ ·}g·····X}g····· │ 0x0000000002677d20 abababab abababab abababab abababab │ ················ │ 0x0000000002677d30 00000000 00000000 00000000 00000000 │ ················ │ 0x0000000002677d40 eefeeefe eefeeefe 581cbfec 7b7b0430 │ ········X···{{·0 │ 0x0000000002677d50 0d000000 04080000 30aa4700 00000000 │ ········0·G····· │ 0x0000000002677d60 abababab abababab abababab abababab │ ················ │ 0x0000000002677d70 00000000 00000000 00000000 00000000 │ ················ │ 0x0000000002677d80 00000000 00000000 581cbfec 7b7b0437 │ ········X···{{·7 │ 0x0000000002677d90 00000000 00000000 00ababab abababab │ ················ │ 0x0000000002677da0 abababab abababab ab000000 00000000 │ ················ │ 0x0000000002677db0 00000000 00000000 00000000 00000000 │ ················ │ 0x0000000002677dc0 00000000 00000000 7c1cbccb 7b7b0400 │ ········|···{{·· │ 0x0000000002677dd0 b05f0201 00000000 e0200201 00000000 │ ·_······· ······ │ 0x0000000002677de0 eefeeefe eefeeefe eefeeefe eefeeefe │ ················ │ 0x0000000002677df0 eefeeefe eefeeefe eefeeefe eefeeefe │ ················ │ CPointer<T> NativePtr 0x02677dd0 T = IntVar CValue<T> ByteArray at 0x0ff75012 eefeeefe eefeeefe 581cbfec 7b7b0430 0d000000 04080000 30aa4700 00000000 abababab abababab abababab abababab ...
  • 49. A Kotlin function’s C type is a CFunction It can be obtained by calling staticCFunction It only works with unbound functions • So instance method references will not work (as they capture the object) Lambda and static function references are both supported Passing Kotlin Functions into C CPointed NativePointed CVariable CFunction
  • 50. © Instil Software 2019 Multi-Threading
  • 51. To avoid common pitfalls of multi-threading with shared mutable state, Kotlin imposes restrictions on how data can be shared between threads Only one thread may own mutable data • Ownership can be transferred Only immutable data or ‘frozen’ data can be shared between threads • Any writes to frozen data results in an ‘InvalidMutabilityException’ Constraints in Sharing Data Shared Mutable XOR ⊕
  • 52. Only Frozen or Immutable data can be safely shared between threads Singleton objects and enums are automatically frozen Arbitrary objects can be frozen via the ‘freeze’ method Global variables can be marked with @SharedImmutable Global variables can also be marked with @ThreadLocal • In which case each thread gets its own copy of the data Freezing
  • 53. These constraints apply to Kotlin data • Natively allocated data will not have these restrictions imposed There are also atomic constructs which can be mutated • AtomicInt • AtomicDouble • Etc Obviously, if using these constructs then you have all the potential pitfalls • Race conditions, deadlocks etc Breaking the Rules
  • 54. © Instil Software 2019 Coroutines
  • 55. A good ecosystem is essential for rapid development As a lot of Kotlin code is multi-platform by default, many libraries can be consumed by Kotlin Native • Sometimes only partially support A good example of this is the coroutines library Using Libraries
  • 56. “Coroutines are computer-program components that generalize subroutines for non-preemptive multitasking, by allowing multiple entry points for suspending and resuming execution at certain locations.” Wikipedia Coroutine
  • 57. 1 1 2 2 3 3 4 4 5 5 6 6 7 7
  • 59. Consider 3 independent jobs of work Event Loop with Asynchronous Blocks Start 5 6EndBlocking Start 5 6EndBlocking Start 5 6EndBlocking Start 5 6EndBlocking Start 5 6EndBlocking Start 5 6EndBlocking If we want to run these in parallel we could create multiple threads
  • 60. Asynchronous Use Case – Thread Pools Start End Blocking Start End Blocking Start 5 6End Blocking
  • 61. Coroutines as Light Weight Threads Start EndBlocking Start EndBlocking Start 5 6EndBlocking
  • 62. Coroutines Scale Start EndBlocking Start EndBlocking Start 5 6EndBlocking Start EndBlocking Start EndBlocking Start 5 6EndBlocking Start EndBlocking
  • 63. © Instil Software 2019 • Objective C Interop Swift Interop
  • 64. Swift itself is not supported but Objective C is supported Swift APIs can be exported to Objective C by annotating with @objc Bidirectional interop with Objective C is more intuitive & complete than C • Classes and interfaces/protocols can be used • Reference counting and garbage collection are integrated • More types are automatically mapped e.g. List Objective C & Swift Interop
  • 65. We will focus on the interop story with Swift rather than Objective C directly • It is a more modern language and the obvious choice for new development Since Swift is translated to Objective C, we immediately lose features • Generics • Tuples • Enumerations defined in Swift without Int raw value type • Structures defined in Swift • Top-level functions and global variables defined in Swift • Typealiases defined in Swift • Swift-style variadics • Nested types • Curried functions Objective C & Swift Interop
  • 66. We can also compile Kotlin into Apple frameworks and libraries Under this scenario we do not have support for, • Suspend functions • Inline classes But many things map intuitively Objective C & Swift Interop
  • 67. Note that for elements to get translated, they must be, • Annotated with @objc • Public within a public class Translations @objc public class Employee : NSObject { @objc public let name: String @objc public var age: Int @objc public var salary: Double public let noObjC = "" @objc let notPublic = "" open class Employee : NSObject { @ObjCMethod("name", "@16@0:8") external open fun name(): String @ObjCMethod("age", "q16@0:8") external open fun age(): NSInteger @ObjCMethod("setAge:", "v24@0:8q16") external open fun setAge(age: NSInteger): Unit @ObjCMethod("salary", "d16@0:8") external open fun salary(): Double @ObjCMethod("setSalary:", "v24@0:8d16") external open fun setSalary(salary: Double): Unit // ...
  • 68. Mappings Kotlin Swift Objective-C class class @interface interface protocol @protocol constructor/create Initializer Initializer Property Property Property Method Method Method @Throws throws error:(NSError**)error Extension Extension Category member companion member <- Class method or property Class method or property null nil nil Singleton Singleton() [Singleton singleton]
  • 69. Kotlin Swift Objective-C Primitive type Primitive type / NSNumber Unit return type Void void String String NSString String NSMutableString NSMutableString List Array NSArray MutableList NSMutableArray NSMutableArray Set Set NSSet MutableSet NSMutableSet NSMutableSet Map Dictionary NSDictionary MutableMap NSMutableDictionary NSMutableDictionary Function type Function type Block pointer type Mapping
  • 70. Function types also have good support including closures Note that primitive parameters will be boxed Type differences may cause function reference issues • E.g. Expecting an a Long parameter for an Int parameter Function Types @objc public func filter(numbers: [Int], predicate: (Int) -> Bool) -> [Int] external open fun filter(numbers: List<*>, predicate: (NSInteger) -> Boolean): List<*> Swift Kotlin
  • 71. © Instil Software 2019 Summary
  • 72. C++ interop is not there, only via C Swift interop is good but not fully there • Since it is translating via Objective C Build times are slow - especially when compared with modern languages • E.g. Kotlin/Java on JVM, TypeScript, C# The Bad
  • 73. Still beta so there are some issues • In Windows the IDE keeps locking the klib so I need to restart • Autocomplete not in CLion for Gradle KTS • Documentation incomplete or out of date Encountered memory leak that I couldn’t quite track down The Bad
  • 74. With C, you will need to switch between Strings and pointers to Byte • It will depend on the usage case • E.g. function calls, struct fields, function pointers You miss some of the features of the Java Standard Library • Posix give some cross platform features but … • There are differences in the APIs across platforms • They are not Kotlin first so the usage is clunky Performance is not there • Generally when compared with say a JVM implementation • CValues can have severe performance impact The Bad
  • 75. The Kotlin language is excellent so using in more environments is good The tooling around code authoring is excellent • Though documentation and debugging is not there yet Interop with C and Objective C is fairly good • Especially Swift/Objective C with memory management, closures etc. The potential for sharing code across platforms is appealing The Good
  • 76. 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 Summary
  • 77. 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 write once, run anywhere language Summary Well….maybe

Editor's Notes

  • #61: If the pools are created then dispatch time is faster