kot·lin mul·ti·plat·form /ˌkätˈlin məltiˈplatfôrm,ˌkätˈlin məltīˈplatfôrm/ noun noun: kotlin multiplatform 1.optional, natively-integrated, open-source, code sharing platform, based on the popular, modern language kotlin. facilitates non-ui logic availability on many platforms.
fun printStuff2() { val someData = SomeData("Hello ", 2) worker.execute(TransferMode.SAFE, { someData }) { println(it) }.result } execute wants to transfer someData between threads
fun printStuff2() { val someData = SomeData("Hello ", 2) worker.execute(TransferMode.SAFE, { someData }) { println(it) }.result } local is still a ref till the end
data class SomeData(val s:String, val i:Int) fun printStuff2() { val someData = SomeData("Hello ", 2) worker.execute(TransferMode.SAFE, { someData }) { println(it) }.result } Mutable state?
Freeze • Recursively freezes everything • One-way (no “unfreeze”) • Data may be shared between threads Int String Float Float String Int MoreData val strData var width SomeData val moreData var count
data class SomeData(val s:String, val i:Int) fun printStuff2() { val someData = SomeData("Hello ", 2) worker.execute(TransferMode.SAFE, { someData }) { println(it) }.result }
data class SomeData(val s:String, val i:Int) fun printStuff2() { val someData = SomeData("Hello ", 2) worker.execute(TransferMode.SAFE, { someData.freeze() }) { println(it) }.result }
data class SomeData(val s:String, val i:Int) fun printStuff2() { val someData = SomeData("Hello ", 2) worker.execute(TransferMode.SAFE, { someData.freeze() }) { println(it) }.result }
data class SomeData(val s:String, val i:Int) fun printStuff2() { val someData = SomeData("Hello ", 2) worker.execute(TransferMode.SAFE, { someData.freeze() }) { println(it) }.result }
data class SomeData(val s:String, val i:Int) fun printStuff2() { val someData = SomeData("Hello ", 2).freeze() worker.execute(TransferMode.SAFE, { }) { println(someData) }.result } Worker.execute must take an unbound, non-capturing function or lambda
fun background(block:(Unit)->Unit){ worker.execute(TransferMode.SAFE, {}, block) } Worker.execute must take an unbound, non-capturing function or lambda
fun background(block:(Unit)->Unit){ worker.execute(TransferMode.SAFE, {}, block.freeze()) } Worker.execute must take an unbound, non-capturing function or lambda
Overview coroutine always bound to a single thread create with newSingleThreadContext on native that uses Worker Default has single background thread Main defined for Apple targets Windows/Linux ¯\_(ツ)_/¯
Samples from slides https://github.com/kpgalligan/MTCoroutines Droidcon app https://github.com/touchlab/DroidconKotlin/ (look for the kpg/mt_coroutines branch)