Share
Embed
Beyond Kotlin - Advanced features for API Makers
Kotlin talk for experimented developers
Arnaud GIULIANI
September 30, 2017
Transcript
152 slides
-
Work @ ekito Mobile & Cloud Kotlin Lover @arnogiu Arnaud
GIULIANI medium.com/@giuliani.arnaud/ ekito.fr/people #DevFestToulouse -
h5ps:/ /spring.io/blog/2017/01/04/introducing-kotlin-support-in-spring- framework-5-0 h5p:/ /www.javamagazine.mozaicreader.com/ #&pageSet=5&page=0&contentItem=0 (March/ April 2017) h5ps:/
/www.thoughtworks.com/radar/languages-and-frameworks/kotlin -
Many well-known companies are using Kotlin: Pinterest, Coursera, NeUlix, Uber,
Square, Trello, Basecamp, amongst others well-known banks (such as Goldman Sachs, Wells Fargo, J.P. Morgan, Deutsche Bank, UBS, HSBC, BNP Paribas, Société Générale) -
KOTLIN is not just a syntactic sugar It’s all about
writing SAFER & BETTER APPS ! -
val / var Null safety Class / Object Lambda Functions
Data Class Properties & delegates Default Values Named Parameters Extension Functions InterOp Not today! -
// A Bean Definition data class BeanDefinition(val name: String, val
clazz: KClass<*>) like dependency injection An advanced API development use case Let’s take -
// A Bean definition val beanDef: BeanDefinition? // let beanDef?.let
{ println("bean name is '${it.name}'") } // let & assign value val complexName: String? = beanDef?.let { "name : ${it.name} & class ${it.clazz}" } Safely executing with let // A Bean definition val beanDef: BeanDefinition? // let beanDef?.let { println("bean name is '${it.name}'") } -
// takeIf (validate predicate) val bean = beanDef?.takeIf { it.name.isNotEmpty()
} val bean = beanDef?.takeUnless { it.name.isNullOrEmpty() } // A Bean definition val beanDef: BeanDefinition? = ... // Guard like expression val bean: BeanDefinition = beanDef?.takeIf { it.name.isNotEmpty() } ?: error("bean name is empty") // Guard like expression val bean: BeanDefinition = beanDef?.takeIf { it.name.isNotEmpty() } ?: error("bean name is empty") val bean: BeanDefinition = beanDef?.takeIf { it.name.isNotEmpty() } ?: return // takeIf (validate predicate) val bean = beanDef?.takeIf { it.name.isNotEmpty() } val bean = beanDef?.takeUnless { it.name.isNullOrEmpty() } takeIf & takeUnless // A Bean definition val beanDef: BeanDefinition? = ... -
let ~ run -> return last value also ~ apply
-> return itself with() -> function & return last value it let ~ run -> return last value also ~ apply -> return itself with() -> function & return last value this -
Lambda function Receiver Type fun T.function( (T) -> R) fun
T.function( T.() -> R) Writing encapsulation public inline fun <T> T.apply(block: T.() -> Unit): T { block(); return this } public inline fun <T> T.also(block: (T) -> Unit): T { block(this); return this } public inline fun <T> T.apply(block: T.() -> Unit): T { block(); return this } public inline fun <T> T.also(block: (T) -> Unit): T { block(this); return this } public inline fun <T> T.apply(block: T.() -> Unit): T { block(); return this } public inline fun <T> T.also(block: (T) -> Unit): T { block(this); return this } public inline fun <T> T.apply(block: T.() -> Unit): T { block(); return this } public inline fun <T> T.also(block: (T) -> Unit): T { block(this); return this } -
SAM Conversion public class JavaBeanDefinition { String name; Class clazz;
public JavaBeanDefinition(String name, Class clazz) {...} public void postInit(JavaInitializingBean initBean){ // Register post init } } public interface JavaInitializingBean { void onInitDone(); } val clazz = MyService::class.java val javaBean = JavaBeanDefinition(clazz.simpleName,clazz) public class JavaBeanDefinition { String name; Class clazz; public JavaBeanDefinition(String name, Class clazz) {...} public void postInit(JavaInitializingBean initBean){ // Register post init } } public interface JavaInitializingBean { void onInitDone(); } val clazz = MyService::class.java val javaBean = JavaBeanDefinition(clazz.simpleName,clazz) javaBean.postInit { println("bean has been defined ! ") } JAVA JAVA -
The functional way // A Bean definition with post init
data class BeanDefinition(val name: String, val clazz: KClass<*>) { fun postInit(initializingBean: InitializingBean) { // register post init ... } } // A Bean definition with post init data class BeanDefinition(val name: String, val clazz: KClass<*>) { fun postInit(initializingBean: () -> Unit) { // register post init ... } } val clazz = MyService::class val bean = BeanDefinition(clazz.java.simpleName, clazz) val clazz = MyService::class val bean = BeanDefinition(clazz.java.simpleName, clazz) bean.postInit { println("bean has been defined ! ") } -
Dealing with generics sealed class BeanDefinition(val name : String, val
clazz : KClass<*>) class Singleton(n : String, c : KClass<*>) : BeanDefinition(n,c) class Factory(n : String, c : KClass<*>) : BeanDefinition(n,c) fun <T> registerBean(def: T) { //... } // Limit with Bounds fun <T : BeanDefinition> registerBean(def: T) { //... } -
Dealing with generics sealed class BeanDefinition(val name : String, val
clazz : KClass<*>) class Singleton(n : String, c : KClass<*>) : BeanDefinition(n,c) class Factory(n : String, c : KClass<*>) : BeanDefinition(n,c) fun <T> registerBean(def: T) { //... } class BeanProvider<T : BeanDefinition> { } // Limit with Bounds fun <T : BeanDefinition> registerBean(def: T) { //... } // Limit with Bounds fun <T : BeanDefinition> registerBean(def: T) { //... } sealed class BeanDefinition(val name : String, val clazz : KClass<*>) class Singleton(n : String, c : KClass<*>) : BeanDefinition(n,c) class Factory(n : String, c : KClass<*>) : BeanDefinition(n,c) -
Reified Types fun declareBean(name :String, clazz : KClass<*>){ val bean
= BeanDefinition(name,clazz) } -
Reified Types fun declareBean(name :String, clazz : KClass<*>){ val bean
= BeanDefinition(name,clazz) } fun <T> declareBean(name :String, clazz : T){ val bean = BeanDefinition(name,???) } fun <T> declareBean(name :String, clazz : T){ // Capture Type parameter class val clazz = T::class val bean = BeanDefinition(name,clazz) } -
Reified Types fun declareBean(name :String, clazz : KClass<*>){ val bean
= BeanDefinition(name,clazz) } fun <T> declareBean(name :String, clazz : T){ val bean = BeanDefinition(name,???) } fun <T> declareBean(name :String, clazz : T){ // Capture Type parameter class val clazz = T::class val bean = BeanDefinition(name,clazz) } inline fun <reified T> declareBean(name :String, clazz : T){ // Capture Type parameter class val clazz = T::class val bean = BeanDefinition(name,clazz) } -
Reified Types fun declareBean(name :String, clazz : KClass<*>){ val bean
= BeanDefinition(name,clazz) } fun <T> declareBean(name :String, clazz : T){ val bean = BeanDefinition(name,???) } fun <T> declareBean(name :String, clazz : T){ // Capture Type parameter class val clazz = T::class val bean = BeanDefinition(name,clazz) } inline fun <reified T> declareBean(name :String, clazz : T){ // Capture Type parameter class val clazz = T::class val bean = BeanDefinition(name,clazz) } inline fun <reified T> declareBean(){ // Capture Type parameter class val clazz = T::class val name = clazz.simpleName ?: "" val bean = BeanDefinition(name,clazz) } -
Reified Types fun declareBean(name :String, clazz : KClass<*>){ val bean
= BeanDefinition(name,clazz) } fun <T> declareBean(name :String, clazz : T){ val bean = BeanDefinition(name,???) } fun <T> declareBean(name :String, clazz : T){ // Capture Type parameter class val clazz = T::class val bean = BeanDefinition(name,clazz) } inline fun <reified T> declareBean(name :String, clazz : T){ // Capture Type parameter class val clazz = T::class val bean = BeanDefinition(name,clazz) } inline fun <reified T> declareBean(){ // Capture Type parameter class val clazz = T::class val name = clazz.simpleName ?: "" val bean = BeanDefinition(name,clazz) } inline fun <reified T> declareBean(){ // Capture Type parameter class val clazz = T::class val name = clazz.simpleName ?: "" val bean = BeanDefinition(name,clazz) } declareBean<MyService>() -
Type Aliases typealias BeanList = List<BeanDefinition> typealias BeanList = List<BeanDefinition>
val list : BeanList = listOf() -
Type Aliases typealias BeanList = List<BeanDefinition> typealias BeanList = List<BeanDefinition>
val list : BeanList = listOf() typealias BeanValidator = (BeanDefinition) -> Boolean typealias BeanList = List<BeanDefinition> val list : BeanList = listOf() typealias BeanValidator = (BeanDefinition) -> Boolean val bv : BeanValidator = { def -> def.name.isNotEmpty()} -
Type Aliases typealias BeanList = List<BeanDefinition> typealias BeanList = List<BeanDefinition>
val list : BeanList = listOf() typealias BeanValidator = (BeanDefinition) -> Boolean typealias BeanList = List<BeanDefinition> val list : BeanList = listOf() typealias BeanValidator = (BeanDefinition) -> Boolean val bv : BeanValidator = { def -> def.name.isNotEmpty()} typealias BeanValidator = (BeanDefinition) -> Boolean val bv : BeanValidator = { def -> def.name.isNotEmpty()} // A Bean definition with post init data class BeanDefinition(val name: String, val clazz: KClass<*>) { fun validate(validator: BeanValidator) : Boolean = validator(this) } // A Bean definition with post init data class BeanDefinition(val name: String, val clazz: KClass<*>) { fun validate(validator: BeanValidator) : Boolean = validator(this) } -
Type Aliases typealias BeanList = List<BeanDefinition> typealias BeanList = List<BeanDefinition>
val list : BeanList = listOf() typealias BeanValidator = (BeanDefinition) -> Boolean typealias BeanList = List<BeanDefinition> val list : BeanList = listOf() typealias BeanValidator = (BeanDefinition) -> Boolean val bv : BeanValidator = { def -> def.name.isNotEmpty()} typealias BeanValidator = (BeanDefinition) -> Boolean val bv : BeanValidator = { def -> def.name.isNotEmpty()} // A Bean definition with post init data class BeanDefinition(val name: String, val clazz: KClass<*>) { fun validate(validator: BeanValidator) : Boolean = validator(this) } // A Bean definition with post init data class BeanDefinition(val name: String, val clazz: KClass<*>) { fun validate(validator: BeanValidator) : Boolean = validator(this) } // A Bean definition with post init data class BeanDefinition(val name: String, val clazz: KClass<*>) { fun validate(validator: BeanValidator) : Boolean = validator(this) } -
Making clean syntax StringUtil.capitalize(s) s.capitalize() Extension FuncNon 1.to("one") 1 to
"one" Infix call set.add(2) set += 2 Operator overloading map.get("key") map["key"] Get method convenNon StringUtil.capitalize(s) 1.to("one") set.add(2) map.get("key") -
file.use({f -> f.read()}) file.use {f -> f.read()} Lambda outside parenthesis
sb.append("yes") sb.append("no") with (sb){ append("yes") append("no") } Lambda with receiver file.use({f -> f.read()}) sb.append("yes") sb.append("no") Making clean syntax -
DSL - a small set of features - focus on
a particular task declarative API - a set of functions and procedures - for creation of applications imperative => internal DSL -
provide { MyService() } Builder function Type reference () ->
MyService MyService::class fun provide( definition : () -> T ) -
provide { MyServiceA() } declareContext { } provide { MyServiceB(
? ) } provide { MyServiceC( ? , ?) } data class MyServiceA() data class MyServiceB(val a : MyServiceA) data class MyServiceC(val a : MyServiceA, val b : MyServiceB) provide { MyServiceC(get<MyServiceA>(),get<MyServiceB>()) } provide { MyServiceB(get<MyServiceA>()) } -
fun <T> provide(definition: () -> T) { } data class
BeanDefinition(val name: String, val clazz: KClass<*>) -
fun <T> provide(definition: () -> T) { val clazz =
T::class val name = clazz.java.simpleName val bean = BeanDefinition(name,clazz) } data class BeanDefinition(val name: String, val clazz: KClass<*>) -
inline fun <reified T> provide(definition: () -> T) { val
clazz = T::class val name = clazz.java.simpleName val bean = BeanDefinition(definition, name, clazz) } data class BeanDefinition(val name: String, val clazz: KClass<*>) -
inline fun <reified T> provide(definition: () -> T) { val
clazz = T::class val name = clazz.java.simpleName val bean = BeanDefinition(definition, name, clazz) } data class BeanDefinition<T>(val definition: () -> T, val name : String, val clazz : KClass<*>) -
inline fun <reified T> provide(noinline definition: () -> T) {
val clazz = T::class val name = clazz.java.simpleName val bean = BeanDefinition(definition, name, clazz) } data class BeanDefinition<T>(val definition: () -> T, val name : String, val clazz : KClass<*>) -
class Context { inline fun <reified T> provide(noinline definition: ()
-> T) { val clazz = T::class val name = clazz.java.simpleName val bean = BeanDefinition(definition, name, clazz) } } data class BeanDefinition<T>(val definition: () -> T, val name : String, val clazz : KClass<*>) -
class Context { var definitions = listOf<BeanDefinition<*>>() inline fun <reified
T> provide(noinline definition: () -> T) { val clazz = T::class val name = clazz.java.simpleName definitions += BeanDefinition(definition, name, clazz) } } data class BeanDefinition<T>(val definition: () -> T, val name : String, val clazz : KClass<*>) -
class Context { var definitions = listOf<BeanDefinition<*>>() inline fun <reified
T> provide(noinline definition: () -> T) { val clazz = T::class val name = clazz.java.simpleName definitions += BeanDefinition(definition, name, clazz) } } data class BeanDefinition<T>(val definition: () -> T, val name : String, val clazz : KClass<*>) fun declareContext(init: Context.() -> Unit) = Context().apply(init) -
class Context { var definitions = listOf<BeanDefinition<*>>() inline fun <reified
T> provide(noinline definition: () -> T) { val clazz = T::class val name = clazz.java.simpleName definitions += BeanDefinition(definition, name, clazz) } } data class BeanDefinition<T>(val definition: () -> T, val name : String, val clazz : KClass<*>) fun declareContext(init: Context.() -> Unit) = Context().apply(init) -
class Context { var definitions = listOf<BeanDefinition<*>>() inline fun <reified
T> provide(noinline definition: () -> T) { val clazz = T::class val name = clazz.java.simpleName definitions += BeanDefinition(definition, name, clazz) } } data class BeanDefinition<T>(val definition: () -> T, val name : String, val clazz : KClass<*>) fun declareContext(init: Context.() -> Unit) = Context().apply(init) -
declareContext { } provide { MyServiceB( ? ) } provide
{ MyServiceC( ? , ?) } data class MyServiceA() data class MyServiceB(val a : MyServiceA) data class MyServiceC(val a : MyServiceA, val b : MyServiceB) provide { MyServiceA() } provide { MyServiceC(get<MyServiceA>(),get<MyServiceB>()) } provide { MyServiceB(get<MyServiceA>()) } -
class Context { var definitions = listOf<BeanDefinition<*>>() inline fun <reified
T> provide(noinline definition: () -> T) { val clazz = T::class val name = clazz.java.simpleName definitions += BeanDefinition(definition, name, clazz) } } data class BeanDefinition<T>(val definition: () -> T, val name: String, val clazz: KClass<*>) fun declareContext(init: Context.() -> Unit) = Context().apply(init) -
data class BeanDefinition<T>(val definition: () -> T, val name: String,
val clazz: KClass<*>) fun declareContext(init: Context.() -> Unit) = Context().apply(init) class Context { var definitions = listOf<BeanDefinition<*>>() inline fun <reified T> provide(noinline definition: () -> T) { val clazz = T::class val name = clazz.java.simpleName definitions += BeanDefinition(definition, name, clazz) } } var instances = HashMap<KClass<*>,Any>() -
data class BeanDefinition<T>(val definition: () -> T, val name: String,
val clazz: KClass<*>) fun declareContext(init: Context.() -> Unit) = Context().apply(init) class Context { var definitions = listOf<BeanDefinition<*>>() inline fun <reified T> provide(noinline definition: () -> T) { val clazz = T::class val name = clazz.java.simpleName definitions += BeanDefinition(definition, name, clazz) } fun <T> get() : T{} } var instances = HashMap<KClass<*>,Any>() -
data class BeanDefinition<T>(val definition: () -> T, val name: String,
val clazz: KClass<*>) fun declareContext(init: Context.() -> Unit) = Context().apply(init) class Context { var definitions = listOf<BeanDefinition<*>>() inline fun <reified T> provide(noinline definition: () -> T) { val clazz = T::class val name = clazz.java.simpleName definitions += BeanDefinition(definition, name, clazz) } fun <T> get() : T = instances[T::class] } var instances = HashMap<KClass<*>,Any>() -
data class BeanDefinition<T>(val definition: () -> T, val name: String,
val clazz: KClass<*>) fun declareContext(init: Context.() -> Unit) = Context().apply(init) class Context { var definitions = listOf<BeanDefinition<*>>() inline fun <reified T> provide(noinline definition: () -> T) { val clazz = T::class val name = clazz.java.simpleName definitions += BeanDefinition(definition, name, clazz) } inline fun <reified T> get() : T = instances[T::class] } var instances = HashMap<KClass<*>,Any>() -
data class BeanDefinition<T>(val definition: () -> T, val name: String,
val clazz: KClass<*>) fun declareContext(init: Context.() -> Unit) = Context().apply(init) class Context { var definitions = listOf<BeanDefinition<*>>() inline fun <reified T> provide(noinline definition: () -> T) { val clazz = T::class val name = clazz.java.simpleName definitions += BeanDefinition(definition, name, clazz) } inline fun <reified T> get() : T = instances[T::class] as T } var instances = HashMap<KClass<*>,Any>() -
provide { MyServiceA() } declareContext { } provide { MyServiceB(
? ) } provide { MyServiceC( ? , ?) } data class MyServiceA() : MyServiceA data class MyServiceB(val a : MyServiceA) data class MyServiceC(val a : MyServiceA, val b : MyServiceB) provide { MyServiceC(get<MyInterface>(),get<MyServiceB>()) } provide { MyServiceB(get<MyServiceA>()) } -
provide { MyServiceA() } declareContext { } provide { MyServiceB(
? ) } provide { MyServiceC( ? , ?) } data class MyServiceA() : MyServiceA data class MyServiceB(val a : MyServiceA) data class MyServiceC(val a : MyServiceA, val b : MyServiceB) provide { MyServiceC(get<MyInterface>(),get<MyServiceB>()) } provide { MyServiceB(get<MyServiceA>()) } Lazy evaluated by nature! -
provide { MyServiceA() } declareContext { } data class MyServiceA()
: MyServiceA data class MyServiceB(val a : MyServiceA) data class MyServiceC(val a : MyServiceA, val b : MyServiceB) provide { MyServiceC(get(),get()) } provide { MyServiceB(get()) } -
data class BeanDefinition<T>(val definition: () -> T, val name: String,
val clazz: KClass<*>) fun declareContext(init: Context.() -> Unit) = Context().apply(init) class Context { var definitions = listOf<BeanDefinition<*>>() inline fun <reified T> provide(noinline definition: () -> T) { val clazz = T::class val name = clazz.java.simpleName definitions += BeanDefinition(definition, name, clazz) } inline fun <reified T> get() : T = instances[T::class] as T } var instances = HashMap<KClass<*>,Any>() -
data class BeanDefinition<T>(val definition: () -> T, val name: String,
val clazz: KClass<*>) fun declareContext(init: Context.() -> Unit) = Context().apply(init) class Context {...} var instances = HashMap<KClass<*>,Any>() -
data class BeanDefinition<T>(val definition: () -> T, val name: String,
val clazz: KClass<*>) abstract class Module { abstract fun context() : Context } class Context {...} var instances = HashMap<KClass<*>,Any>() -
data class BeanDefinition<T>(val definition: () -> T, val name: String,
val clazz: KClass<*>) abstract class Module { abstract fun context() : Context fun declareContext(init: Context.() -> Unit) = Context().apply(init) } class Context {...} var instances = HashMap<KClass<*>,Any>() -
class SimpleModule : Module() { override fun context() = declareContext
{ provide { ServiceA() } provide { ServiceB(get()) } provide { ServiceC(get(), get()) } } } data class MyServiceA() data class MyServiceB(val a : MyServiceA) data class MyServiceC(val a : MyServiceA, val b : MyServiceB) Entirely declarative -
data class BeanDefinition<T>(val definition: () -> T, val name: String,
val clazz: KClass<*>) abstract class Module { abstract fun context() : Context fun declareContext(init: Context.() -> Unit) = Context().apply(init) } class Context {...} var instances = HashMap<KClass<*>,Any>() -
data class BeanDefinition<T>(val definition: () -> T, val name: String,
val clazz: KClass<*>) class Context {...} abstract class Module { abstract fun context() : Context fun declareContext(init: Context.() -> Unit) = Context().apply(init) } class CoreContext{ var instances = HashMap<KClass<*>,Any>() var definitions = listOf<BeanDefinition<*>>() } -
data class BeanDefinition<T>(val definition: () -> T, val name: String,
val clazz: KClass<*>) class Context {...} abstract class Module(){ lateinit var coreContext: CoreContext abstract fun context() : Context fun declareContext(init: Context.() -> Unit) = Context(coreContext).apply(init) } class CoreContext{ var instances = HashMap<KClass<*>,Any>() var definitions = listOf<BeanDefinition<*>>() } -
data class BeanDefinition<T>(val definition: () -> T, val name: String,
val clazz: KClass<*>) class Context {...} abstract class Module(){ lateinit var coreContext: CoreContext abstract fun context() : Context fun declareContext(init: Context.() -> Unit) = Context(coreContext).apply(init) } class CoreContext{ var instances = HashMap<KClass<*>,Any>() var definitions = listOf<BeanDefinition<*>>() } -
data class BeanDefinition<T>(val definition: () -> T, val name: String,
val clazz: KClass<*>) class Context(val coreContext: CoreContext) { ... } abstract class Module(){ lateinit var coreContext: CoreContext abstract fun context() : Context fun declareContext(init: Context.() -> Unit) = Context(coreContext).apply(init) } class CoreContext{ var instances = HashMap<KClass<*>,Any>() var definitions = listOf<BeanDefinition<*>>() } -
class Context(val coreContext: CoreContext) { //... inline fun <reified T>
get() : T = instances[T::class] } -
class Context(val coreContext: CoreContext) { //... inline fun <reified T>
get() : T = coreContext.inject() } -
class CoreContext { var instances = HashMap<KClass<*>, Any>() var definitions
= listOf<BeanDefinition<*>>() fun <T> inject(): T {} } -
class CoreContext { var instances = HashMap<KClass<*>, Any>() var definitions
= listOf<BeanDefinition<*>>() fun <T> inject(): T { val clazz = T::class } } -
class CoreContext { var instances = HashMap<KClass<*>, Any>() var definitions
= listOf<BeanDefinition<*>>() inline fun <reified T> inject(): T { val clazz = T::class } } -
class CoreContext { var instances = HashMap<KClass<*>, Any>() var definitions
= listOf<BeanDefinition<*>>() inline fun <reified T> inject(): T { val clazz = T::class // found one ? val foundInstance: T? = instances[clazz] as? T } } -
class CoreContext { var instances = HashMap<KClass<*>, Any>() var definitions
= listOf<BeanDefinition<*>>() inline fun <reified T> inject(): T { val clazz = T::class // found one ? val foundInstance: T? = instances[clazz] as? T // create one ? val createdInstance: T? = if (foundInstance == null) { definitions.firstOrNull { it.clazz == clazz }?.let { it.definition.invoke() as? T? } } else null } } -
class CoreContext { var instances = HashMap<KClass<*>, Any>() var definitions
= listOf<BeanDefinition<*>>() inline fun <reified T> inject(): T { val clazz = T::class // found one ? val foundInstance: T? = instances[clazz] as? T // create one ? val createdInstance: T? = if (foundInstance == null) { definitions.firstOrNull { it.clazz == clazz }?.let { it.definition.invoke() as? T? } } else null } } -
class CoreContext { var instances = HashMap<KClass<*>, Any>() var definitions
= listOf<BeanDefinition<*>>() inline fun <reified T> inject(): T { val clazz = T::class // found one ? val foundInstance: T? = instances[clazz] as? T // create one ? val createdInstance: T? = if (foundInstance == null) { definitions.firstOrNull { it.clazz == clazz }?.let { it.definition.invoke() as? T? } } else null } } -
class CoreContext { var instances = HashMap<KClass<*>, Any>() var definitions
= listOf<BeanDefinition<*>>() inline fun <reified T> inject(): T { val clazz = T::class // found one ? val foundInstance: T? = instances[clazz] as? T // create one ? val createdInstance: T? = if (foundInstance == null) { definitions.firstOrNull { it.clazz == clazz }?.let { it.definition.invoke() as? T? } } else null } } -
class CoreContext { var instances = HashMap<KClass<*>, Any>() var definitions
= listOf<BeanDefinition<*>>() inline fun <reified T> inject(): T { val clazz = T::class // found one ? val foundInstance: T? = instances[clazz] as? T // create one ? val createdInstance: T? = if (foundInstance == null) { definitions.firstOrNull { it.clazz == clazz }?.let { it.definition.invoke() as? T? } } else null // Got it val instance: T = (foundInstance ?: createdInstance) ?: error("Bean $clazz not found") } } -
class CoreContext { var instances = HashMap<KClass<*>, Any>() var definitions
= listOf<BeanDefinition<*>>() inline fun <reified T> inject(): T { val clazz = T::class // found one ? val foundInstance: T? = instances[clazz] as? T // create one ? val createdInstance: T? = if (foundInstance == null) { definitions.firstOrNull { it.clazz == clazz }?.let { it.definition.invoke() as? T? } } else null // Got it val instance: T = (foundInstance ?: createdInstance) ?: error("Bean $clazz not found ») // Save it if (createdInstance != null && foundInstance == null) { instances[clazz] = createdInstance as Any } } } -
class CoreContext { var instances = HashMap<KClass<*>, Any>() var definitions
= listOf<BeanDefinition<*>>() inline fun <reified T> inject(): T { val clazz = T::class // found one ? val foundInstance: T? = instances[clazz] as? T // create one ? val createdInstance: T? = if (foundInstance == null) { definitions.firstOrNull { it.clazz == clazz }?.let { it.definition.invoke() as? T? } } else null // Got it val instance: T = (foundInstance ?: createdInstance) ?: error("Bean $clazz not found") // Save it if (createdInstance != null && foundInstance == null) { instances[clazz] = createdInstance as Any } return instance } } -
class CoreContext { var instances = HashMap<KClass<*>, Any>() var definitions
= listOf<BeanDefinition<*>>() fun <T : Module> build(module: T) {} inline fun <reified T> inject(): T {} } -
class CoreContext { var instances = HashMap<KClass<*>, Any>() var definitions
= listOf<BeanDefinition<*>>() fun <T : Module> build(module: T) { module.coreContext = this } inline fun <reified T> inject(): T {} } -
class CoreContext { var instances = HashMap<KClass<*>, Any>() var definitions
= listOf<BeanDefinition<*>>() fun <T : Module> build(module: T) { module.coreContext = this definitions += module.context().definitions } inline fun <reified T> inject(): T {} } -
data class MyServiceA() data class MyServiceB(val a : MyServiceA) data
class MyServiceC(val a : MyServiceA, val b : MyServiceB) class SimpleModule : Module() { override fun context() = declareContext { provide { ServiceA() } provide { ServiceB(get()) } provide { ServiceC(get(), get()) } } } Let’s use it -
val ctx = CoreContext() ctx.build(SimpleModule()) data class MyServiceA() data class
MyServiceB(val a : MyServiceA) data class MyServiceC(val a : MyServiceA, val b : MyServiceB) class SimpleModule : Module() { override fun context() = declareContext { provide { ServiceA() } provide { ServiceB(get()) } provide { ServiceC(get(), get()) } } } -
val ctx = CoreContext() ctx.build(SimpleModule()) val serviceB = ctx.inject<MyServiceB>() data
class MyServiceA() data class MyServiceB(val a : MyServiceA) data class MyServiceC(val a : MyServiceA, val b : MyServiceB) class SimpleModule : Module() { override fun context() = declareContext { provide { ServiceA() } provide { ServiceB(get()) } provide { ServiceC(get(), get()) } } } -
Reflection -> Kclass, KProperty, KFunction … -> Java => Extra
lib And also … Extra Binding Lazy Inject -
KEEP immutable collections - « real » immutable collections -
avoid java backed collections New in 1.1 - Array-Like instantiation - onEach() - minOf/maxOf - groupingBy() - Map : minus(), getValue() Collections -
Stream API (Kotlin on Java 8+) - toStream() Collection to
Sequences (Pure Kotlin or Java 6/7) - asSequence() Lazy Collections -
sequence.map {…} .filter {…} .toList() Intermediate operations terminal operation sequence.map
{…} .filter {…} .toList() sequence.map {…} .filter {…} .toList() -
Kotlin >= 1.1.4 Gradle: compile ‘org.jetbrains.kotlinx:kotlinx-coroutines-core:0.18’ Remove warnings: kotlin {
experimental { coroutines "enable" } } -
Suspend - keyword, mark function as « suspending function »
Coroutines - API for computations that can be suspended without blocking a thread - launched with coroutine builder (light-weight threads) -
@Test fun test() = runBlocking { val jobs = List(100_000)
{ launch(CommonPool) { delay(1000L) print(".") } } jobs.forEach { it.join() } } -
@Test fun test() = runBlocking { val jobs = List(100_000)
{ launch(CommonPool) { delay(1000L) print(".") } } jobs.forEach { it.join() } } @Test fun test() = runBlocking { val jobs = List(100_000) { launch(CommonPool) { delay(1000L) print(".") } } jobs.forEach { it.join() } } -
@Test fun test() = runBlocking { val jobs = List(100_000)
{ launch(CommonPool) { delay(1000L) print(".") } } jobs.forEach { it.join() } } @Test fun test() = runBlocking { val jobs = List(100_000) { launch(CommonPool) { delay(1000L) print(".") } } jobs.forEach { it.join() } } @Test fun test() = runBlocking { val jobs = List(100_000) { launch(CommonPool) { delay(1000L) print(".") } } jobs.forEach { it.join() } } -
@Test fun test() = runBlocking { val jobs = List(100_000)
{ launch(CommonPool) { delay(1000L) print(".") } } jobs.forEach { it.join() } } @Test fun test() = runBlocking { val jobs = List(100_000) { launch(CommonPool) { delay(1000L) print(".") } } jobs.forEach { it.join() } } @Test fun test() = runBlocking { val jobs = List(100_000) { launch(CommonPool) { delay(1000L) print(".") } } jobs.forEach { it.join() } } @Test fun test() = runBlocking { val jobs = List(100_000) { launch(CommonPool) { delay(1000L) print(".") } } jobs.forEach { it.join() } } -
@Test fun test() = runBlocking { val jobs = List(100_000)
{ launch(CommonPool) { delay(1000L) print(".") } } jobs.forEach { it.join() } } @Test fun test() = runBlocking { val jobs = List(100_000) { launch(CommonPool) { delay(1000L) print(".") } } jobs.forEach { it.join() } } @Test fun test() = runBlocking { val jobs = List(100_000) { launch(CommonPool) { delay(1000L) print(".") } } jobs.forEach { it.join() } } @Test fun test() = runBlocking { val jobs = List(100_000) { launch(CommonPool) { delay(1000L) print(".") } } jobs.forEach { it.join() } } @Test fun test() = runBlocking { val jobs = List(100_000) { launch(CommonPool) { delay(1000L) print(".") } } jobs.forEach { it.join() } } -
@Test fun test() = runBlocking { val jobs = List(100_000)
{ launch(CommonPool) { delay(1000L) print(".") } } jobs.forEach { it.join() } } @Test fun test() = runBlocking { val jobs = List(100_000) { launch(CommonPool) { delay(1000L) print(".") } } jobs.forEach { it.join() } } @Test fun test() = runBlocking { val jobs = List(100_000) { launch(CommonPool) { delay(1000L) print(".") } } jobs.forEach { it.join() } } @Test fun test() = runBlocking { val jobs = List(100_000) { launch(CommonPool) { delay(1000L) print(".") } } jobs.forEach { it.join() } } @Test fun test() = runBlocking { val jobs = List(100_000) { launch(CommonPool) { delay(1000L) print(".") } } jobs.forEach { it.join() } } @Test fun test() = runBlocking { val main = measureTimeMillis { val jobs = List(100_000) { launch(CommonPool) { delay(1000L) print(".") } } jobs.forEach { it.join() } } println("\ndone in $main") } -
@Test fun test() = runBlocking { val main = measureTimeMillis
{ val jobs = List(100_000) { launch(CommonPool) { doSomething() } } jobs.forEach { it.join() } } println("\ndone in $main") } suspend fun doSomething() { delay(1000L) print(".") } -
suspend fun delay(time: Long, unit: TimeUnit = TimeUnit.MILLISECONDS) {…} suspend
fun delay(time: Long, unit: TimeUnit = TimeUnit.MILLISECONDS) public fun <T> runBlocking(context: CoroutineContext = EmptyCoroutineContext, block: suspend CoroutineScope.() -> T): T {…} -
@Test fun testWeather() = runBlocking { log("starting ...") val ws
= retrofitWS("https://weather-api.herokuapp.com/") try { val location = asyncGeocode(ws).await() ?: error("No location :(") val weather = asyncWeather(location, ws).await() log("got weather : $weather") } catch (e: Exception) { System.err.println("Error is $e") } log("finished !") } private fun asyncWeather(location: Location, ws: BlockingWeatherWS): Deferred<Weather> = async(CommonPool) { log("get weather for $location") ws.weather(location.lat, location.lng, "EN").execute().body() } private fun asyncGeocode(ws: BlockingWeatherWS): Deferred<Location?> = async(CommonPool) { log("get location") ws.geocode("Toulouse,fr").execute().body().results.first().geometry?.location } -
@Test fun testWeather() = runBlocking { log("starting ...") val ws
= retrofitWS("https://weather-api.herokuapp.com/") try { val location = asyncGeocode(ws).await() ?: error("No location :(") val weather = asyncWeather(location, ws).await() log("got weather : $weather") } catch (e: Exception) { System.err.println("Error is $e") } log("finished !") } private fun asyncWeather(location: Location, ws: BlockingWeatherWS): Deferred<Weather> = async(CommonPool) { log("get weather for $location") ws.weather(location.lat, location.lng, "EN").execute().body() } private fun asyncGeocode(ws: BlockingWeatherWS): Deferred<Location?> = async(CommonPool) { log("get location") ws.geocode("Toulouse,fr").execute().body().results.first().geometry?.location } @Test fun testWeather() = runBlocking { log("starting ...") val ws = retrofitWS("https://weather-api.herokuapp.com/") try { val location = asyncGeocode(ws).await() ?: error("No location :(") val weather = asyncWeather(location, ws).await() log("got weather : $weather") } catch (e: Exception) { System.err.println("Error is $e") } log("finished !") } private fun asyncWeather(location: Location, ws: BlockingWeatherWS): Deferred<Weather> = async(CommonPool) { log("get weather for $location") ws.weather(location.lat, location.lng, "EN").execute().body() } private fun asyncGeocode(ws: BlockingWeatherWS): Deferred<Location?> = async(CommonPool) { log("get location") ws.geocode("Toulouse,fr").execute().body().results.first().geometry?.location } -
@Test fun testWeather() = runBlocking { log("starting ...") val ws
= retrofitWS("https://weather-api.herokuapp.com/") try { val location = asyncGeocode(ws).await() ?: error("No location :(") val weather = asyncWeather(location, ws).await() log("got weather : $weather") } catch (e: Exception) { System.err.println("Error is $e") } log("finished !") } private fun asyncWeather(location: Location, ws: BlockingWeatherWS): Deferred<Weather> = async(CommonPool) { log("get weather for $location") ws.weather(location.lat, location.lng, "EN").execute().body() } private fun asyncGeocode(ws: BlockingWeatherWS): Deferred<Location?> = async(CommonPool) { log("get location") ws.geocode("Toulouse,fr").execute().body().results.first().geometry?.location } @Test fun testWeather() = runBlocking { log("starting ...") val ws = retrofitWS("https://weather-api.herokuapp.com/") try { val location = asyncGeocode(ws).await() ?: error("No location :(") val weather = asyncWeather(location, ws).await() log("got weather : $weather") } catch (e: Exception) { System.err.println("Error is $e") } log("finished !") } private fun asyncWeather(location: Location, ws: BlockingWeatherWS): Deferred<Weather> = async(CommonPool) { log("get weather for $location") ws.weather(location.lat, location.lng, "EN").execute().body() } private fun asyncGeocode(ws: BlockingWeatherWS): Deferred<Location?> = async(CommonPool) { log("get location") ws.geocode("Toulouse,fr").execute().body().results.first().geometry?.location } @Test fun testWeather() = runBlocking { log("starting ...") val ws = retrofitWS("https://weather-api.herokuapp.com/") try { val location = asyncGeocode(ws).await() ?: error("No location :(") val weather = asyncWeather(location, ws).await() log("got weather : $weather") } catch (e: Exception) { System.err.println("Error is $e") } log("finished !") } private fun asyncWeather(location: Location, ws: BlockingWeatherWS): Deferred<Weather> = async(CommonPool) { log("get weather for $location") ws.weather(location.lat, location.lng, "EN").execute().body() } private fun asyncGeocode(ws: BlockingWeatherWS): Deferred<Location?> = async(CommonPool) { log("get location") ws.geocode("Toulouse,fr").execute().body().results.first().geometry?.location } -
@Test fun testWeather() = runBlocking { log("starting ...") val ws
= retrofitWS("https://weather-api.herokuapp.com/") try { val location = asyncGeocode(ws).await() ?: error("No location :(") val weather = asyncWeather(location, ws).await() log("got weather : $weather") } catch (e: Exception) { System.err.println("Error is $e") } log("finished !") } private fun asyncWeather(location: Location, ws: BlockingWeatherWS): Deferred<Weather> = async(CommonPool) { log("get weather for $location") ws.weather(location.lat, location.lng, "EN").execute().body() } private fun asyncGeocode(ws: BlockingWeatherWS): Deferred<Location?> = async(CommonPool) { log("get location") ws.geocode("Toulouse,fr").execute().body().results.first().geometry?.location } @Test fun testWeather() = runBlocking { log("starting ...") val ws = retrofitWS("https://weather-api.herokuapp.com/") try { val location = asyncGeocode(ws).await() ?: error("No location :(") val weather = asyncWeather(location, ws).await() log("got weather : $weather") } catch (e: Exception) { System.err.println("Error is $e") } log("finished !") } private fun asyncWeather(location: Location, ws: BlockingWeatherWS): Deferred<Weather> = async(CommonPool) { log("get weather for $location") ws.weather(location.lat, location.lng, "EN").execute().body() } private fun asyncGeocode(ws: BlockingWeatherWS): Deferred<Location?> = async(CommonPool) { log("get location") ws.geocode("Toulouse,fr").execute().body().results.first().geometry?.location } @Test fun testWeather() = runBlocking { log("starting ...") val ws = retrofitWS("https://weather-api.herokuapp.com/") try { val location = asyncGeocode(ws).await() ?: error("No location :(") val weather = asyncWeather(location, ws).await() log("got weather : $weather") } catch (e: Exception) { System.err.println("Error is $e") } log("finished !") } private fun asyncWeather(location: Location, ws: BlockingWeatherWS): Deferred<Weather> = async(CommonPool) { log("get weather for $location") ws.weather(location.lat, location.lng, "EN").execute().body() } private fun asyncGeocode(ws: BlockingWeatherWS): Deferred<Location?> = async(CommonPool) { log("get location") ws.geocode("Toulouse,fr").execute().body().results.first().geometry?.location } @Test fun testWeather() = runBlocking { log("starting ...") val ws = retrofitWS("https://weather-api.herokuapp.com/") try { val location = asyncGeocode(ws).await() ?: error("No location :(") val weather = asyncWeather(location, ws).await() log("got weather : $weather") } catch (e: Exception) { System.err.println("Error is $e") } log("finished !") } private fun asyncWeather(location: Location, ws: BlockingWeatherWS): Deferred<Weather> = async(CommonPool) { log("get weather for $location") ws.weather(location.lat, location.lng, "EN").execute().body() } private fun asyncGeocode(ws: BlockingWeatherWS): Deferred<Location?> = async(CommonPool) { log("get location") ws.geocode("Toulouse,fr").execute().body().results.first().geometry?.location } -
@Test fun testWeather() = runBlocking { log("starting ...") val ws
= retrofitWS("https://weather-api.herokuapp.com/") try { val location = asyncGeocode(ws).await() ?: error("No location :(") val weather = asyncWeather(location, ws).await() log("got weather : $weather") } catch (e: Exception) { System.err.println("Error is $e") } log("finished !") } private fun asyncWeather(location: Location, ws: BlockingWeatherWS): Deferred<Weather> = async(CommonPool) { log("get weather for $location") ws.weather(location.lat, location.lng, "EN").execute().body() } private fun asyncGeocode(ws: BlockingWeatherWS): Deferred<Location?> = async(CommonPool) { log("get location") ws.geocode("Toulouse,fr").execute().body().results.first().geometry?.location } @Test fun testWeather() = runBlocking { log("starting ...") val ws = retrofitWS("https://weather-api.herokuapp.com/") try { val location = asyncGeocode(ws).await() ?: error("No location :(") val weather = asyncWeather(location, ws).await() log("got weather : $weather") } catch (e: Exception) { System.err.println("Error is $e") } log("finished !") } private fun asyncWeather(location: Location, ws: BlockingWeatherWS): Deferred<Weather> = async(CommonPool) { log("get weather for $location") ws.weather(location.lat, location.lng, "EN").execute().body() } private fun asyncGeocode(ws: BlockingWeatherWS): Deferred<Location?> = async(CommonPool) { log("get location") ws.geocode("Toulouse,fr").execute().body().results.first().geometry?.location } @Test fun testWeather() = runBlocking { log("starting ...") val ws = retrofitWS("https://weather-api.herokuapp.com/") try { val location = asyncGeocode(ws).await() ?: error("No location :(") val weather = asyncWeather(location, ws).await() log("got weather : $weather") } catch (e: Exception) { System.err.println("Error is $e") } log("finished !") } private fun asyncWeather(location: Location, ws: BlockingWeatherWS): Deferred<Weather> = async(CommonPool) { log("get weather for $location") ws.weather(location.lat, location.lng, "EN").execute().body() } private fun asyncGeocode(ws: BlockingWeatherWS): Deferred<Location?> = async(CommonPool) { log("get location") ws.geocode("Toulouse,fr").execute().body().results.first().geometry?.location } @Test fun testWeather() = runBlocking { log("starting ...") val ws = retrofitWS("https://weather-api.herokuapp.com/") try { val location = asyncGeocode(ws).await() ?: error("No location :(") val weather = asyncWeather(location, ws).await() log("got weather : $weather") } catch (e: Exception) { System.err.println("Error is $e") } log("finished !") } private fun asyncWeather(location: Location, ws: BlockingWeatherWS): Deferred<Weather> = async(CommonPool) { log("get weather for $location") ws.weather(location.lat, location.lng, "EN").execute().body() } private fun asyncGeocode(ws: BlockingWeatherWS): Deferred<Location?> = async(CommonPool) { log("get location") ws.geocode("Toulouse,fr").execute().body().results.first().geometry?.location } @Test fun testWeather() = runBlocking { log("starting ...") val ws = retrofitWS("https://weather-api.herokuapp.com/") try { val location = asyncGeocode(ws).await() ?: error("No location :(") val weather = asyncWeather(location, ws).await() log("got weather : $weather") } catch (e: Exception) { System.err.println("Error is $e") } log("finished !") } private fun asyncWeather(location: Location, ws: BlockingWeatherWS): Deferred<Weather> = async(CommonPool) { log("get weather for $location") ws.weather(location.lat, location.lng, "EN").execute().body() } private fun asyncGeocode(ws: BlockingWeatherWS): Deferred<Location?> = async(CommonPool) { log("get location") ws.geocode("Toulouse,fr").execute().body().results.first().geometry?.location } -
@Test fun testWeather() = runBlocking { log("starting ...") val ws
= retrofitWS("https://weather-api.herokuapp.com/") try { val location = asyncGeocode(ws).await() ?: error("No location :(") val weather = asyncWeather(location, ws).await() log("got weather : $weather") } catch (e: Exception) { System.err.println("Error is $e") } log("finished !") } private fun asyncWeather(location: Location, ws: BlockingWeatherWS): Deferred<Weather> = async(CommonPool) { log("get weather for $location") ws.weather(location.lat, location.lng, "EN").execute().body() } private fun asyncGeocode(ws: BlockingWeatherWS): Deferred<Location?> = async(CommonPool) { log("get location") ws.geocode("Toulouse,fr").execute().body().results.first().geometry?.location } -
@Test fun testWeather() = runBlocking { log("starting ...") val ws
= retrofitWS("https://weather-api.herokuapp.com/") try { val location = asyncGeocode(ws).await() ?: error("No location :(") val weather = asyncWeather(location, ws).await() log("got weather : $weather") } catch (e: Exception) { System.err.println("Error is $e") } log("finished !") } private fun asyncWeather(location: Location, ws: BlockingWeatherWS): Deferred<Weather> = async(CommonPool) { log("get weather for $location") ws.weather(location.lat, location.lng, "EN").execute().body() } private fun asyncGeocode(ws: BlockingWeatherWS): Deferred<Location?> = async(CommonPool) { log("get location") ws.geocode("Toulouse,fr").execute().body().results.first().geometry?.location } @Test fun testWeather() = runBlocking { log("starting ...") val ws = retrofitWS("https://weather-api.herokuapp.com/") try { val location = asyncGeocode(ws).await() ?: error("No location :(") val weather = asyncWeather(location, ws).await() log("got weather : $weather") } catch (e: Exception) { System.err.println("Error is $e") } log("finished !") } private fun asyncWeather(location: Location, ws: BlockingWeatherWS): Deferred<Weather> = async(CommonPool) { log("get weather for $location") ws.weather(location.lat, location.lng, "EN").execute().body() } private fun asyncGeocode(ws: BlockingWeatherWS): Deferred<Location?> = async(CommonPool) { log("get location") ws.geocode("Toulouse,fr").execute().body().results.first().geometry?.location } -
@Test fun testWeather() = runBlocking { log("starting ...") val ws
= retrofitWS("https://weather-api.herokuapp.com/") try { val location = asyncGeocode(ws).await() ?: error("No location :(") val weather = asyncWeather(location, ws).await() log("got weather : $weather") } catch (e: Exception) { System.err.println("Error is $e") } log("finished !") } private fun asyncWeather(location: Location, ws: BlockingWeatherWS): Deferred<Weather> = async(CommonPool) { log("get weather for $location") ws.weather(location.lat, location.lng, "EN").execute().body() } private fun asyncGeocode(ws: BlockingWeatherWS): Deferred<Location?> = async(CommonPool) { log("get location") ws.geocode("Toulouse,fr").execute().body().results.first().geometry?.location } @Test fun testWeather() = runBlocking { log("starting ...") val ws = retrofitWS("https://weather-api.herokuapp.com/") try { val location = asyncGeocode(ws).await() ?: error("No location :(") val weather = asyncWeather(location, ws).await() log("got weather : $weather") } catch (e: Exception) { System.err.println("Error is $e") } log("finished !") } private fun asyncWeather(location: Location, ws: BlockingWeatherWS): Deferred<Weather> = async(CommonPool) { log("get weather for $location") ws.weather(location.lat, location.lng, "EN").execute().body() } private fun asyncGeocode(ws: BlockingWeatherWS): Deferred<Location?> = async(CommonPool) { log("get location") ws.geocode("Toulouse,fr").execute().body().results.first().geometry?.location } @Test fun testWeather() = runBlocking { log("starting ...") val ws = retrofitWS("https://weather-api.herokuapp.com/") try { val location = asyncGeocode(ws).await() ?: error("No location :(") val weather = asyncWeather(location, ws).await() log("got weather : $weather") } catch (e: Exception) { System.err.println("Error is $e") } log("finished !") } private fun asyncWeather(location: Location, ws: BlockingWeatherWS): Deferred<Weather> = async(CommonPool) { log("get weather for $location") ws.weather(location.lat, location.lng, "EN").execute().body() } private fun asyncGeocode(ws: BlockingWeatherWS): Deferred<Location?> = async(CommonPool) { log("get location") ws.geocode("Toulouse,fr").execute().body().results.first().geometry?.location } -
@Test fun testWeather() = runBlocking { log("starting ...") val ws
= retrofitWS("https://weather-api.herokuapp.com/") try { val location = asyncGeocode(ws).await() ?: error("No location :(") val weather = asyncWeather(location, ws).await() log("got weather : $weather") } catch (e: Exception) { System.err.println("Error is $e") } log("finished !") } private fun asyncWeather(location: Location, ws: BlockingWeatherWS): Deferred<Weather> = async(CommonPool) { log("get weather for $location") ws.weather(location.lat, location.lng, "EN").execute().body() } private fun asyncGeocode(ws: BlockingWeatherWS): Deferred<Location?> = async(CommonPool) { log("get location") ws.geocode("Toulouse,fr").execute().body().results.first().geometry?.location } @Test fun testWeather() = runBlocking { log("starting ...") val ws = retrofitWS("https://weather-api.herokuapp.com/") try { val location = asyncGeocode(ws).await() ?: error("No location :(") val weather = asyncWeather(location, ws).await() log("got weather : $weather") } catch (e: Exception) { System.err.println("Error is $e") } log("finished !") } private fun asyncWeather(location: Location, ws: BlockingWeatherWS): Deferred<Weather> = async(CommonPool) { log("get weather for $location") ws.weather(location.lat, location.lng, "EN").execute().body() } private fun asyncGeocode(ws: BlockingWeatherWS): Deferred<Location?> = async(CommonPool) { log("get location") ws.geocode("Toulouse,fr").execute().body().results.first().geometry?.location } @Test fun testWeather() = runBlocking { log("starting ...") val ws = retrofitWS("https://weather-api.herokuapp.com/") try { val location = asyncGeocode(ws).await() ?: error("No location :(") val weather = asyncWeather(location, ws).await() log("got weather : $weather") } catch (e: Exception) { System.err.println("Error is $e") } log("finished !") } private fun asyncWeather(location: Location, ws: BlockingWeatherWS): Deferred<Weather> = async(CommonPool) { log("get weather for $location") ws.weather(location.lat, location.lng, "EN").execute().body() } private fun asyncGeocode(ws: BlockingWeatherWS): Deferred<Location?> = async(CommonPool) { log("get location") ws.geocode("Toulouse,fr").execute().body().results.first().geometry?.location } @Test fun testWeather() = runBlocking { log("starting ...") val ws = retrofitWS("https://weather-api.herokuapp.com/") try { val location = asyncGeocode(ws).await() ?: error("No location :(") val weather = asyncWeather(location, ws).await() log("got weather : $weather") } catch (e: Exception) { System.err.println("Error is $e") } log("finished !") } private fun asyncWeather(location: Location, ws: BlockingWeatherWS): Deferred<Weather> = async(CommonPool) { log("get weather for $location") ws.weather(location.lat, location.lng, "EN").execute().body() } private fun asyncGeocode(ws: BlockingWeatherWS): Deferred<Location?> = async(CommonPool) { log("get location") ws.geocode("Toulouse,fr").execute().body().results.first().geometry?.location } -
@Test fun testWeather() = runBlocking { log("starting ...") val ws
= retrofitWS("https://weather-api.herokuapp.com/") try { val location = asyncGeocode(ws).await() ?: error("No location :(") val weather = asyncWeather(location, ws).await() log("got weather : $weather") } catch (e: Exception) { System.err.println("Error is $e") } log("finished !") } private fun asyncWeather(location: Location, ws: BlockingWeatherWS): Deferred<Weather> = async(CommonPool) { log("get weather for $location") ws.weather(location.lat, location.lng, "EN").execute().body() } private fun asyncGeocode(ws: BlockingWeatherWS): Deferred<Location?> = async(CommonPool) { log("get location") ws.geocode("Toulouse,fr").execute().body().results.first().geometry?.location } -
@Test fun testWeather() = runBlocking { val ws = retrofitWS("https://my-weather-api.herokuapp.com/")
val time = measureTimeMillis { val location = channelLocation(ws) val weather = channelWeather(location, ws) weather.consumeEach { w -> log("got weather : $w") } location.cancel() weather.cancel() } log("\ndone in $time") } private fun channelWeather(locationChannel: ProducerJob<Location>, ws: BlockingWeatherWS) = produce<Forecastday_>(CommonPool) { log("get weather for $locationChannel") locationChannel.consumeEach { location -> val list = ws.weather(location.lat, location.lng, "EN").execute().body().forecast?.simpleforecast?.forecastday?.take(4).orEmpty() list.forEach { send(it) } } } private fun channelLocation(ws: BlockingWeatherWS) = produce<Location>(CommonPool) { log("get location") val location = ws.geocode("Toulouse,fr").execute().body().results.first().geometry?.location location?.let { send(location) } } -
@Test fun testWeather() = runBlocking { val ws = retrofitWS("https://my-weather-api.herokuapp.com/")
val time = measureTimeMillis { val location = channelLocation(ws) val weather = channelWeather(location, ws) weather.consumeEach { w -> log("got weather : $w") } location.cancel() weather.cancel() } log("\ndone in $time") } private fun channelWeather(locationChannel: ProducerJob<Location>, ws: BlockingWeatherWS) = produce<Forecastday_>(CommonPool) { log("get weather for $locationChannel") locationChannel.consumeEach { location -> val list = ws.weather(location.lat, location.lng, "EN").execute().body().forecast?.simpleforecast?.forecastday?.take(4).orEmpty() list.forEach { send(it) } } } private fun channelLocation(ws: BlockingWeatherWS) = produce<Location>(CommonPool) { log("get location") val location = ws.geocode("Toulouse,fr").execute().body().results.first().geometry?.location location?.let { send(location) } } -
@Test fun testWeather() = runBlocking { val ws = retrofitWS("https://my-weather-api.herokuapp.com/")
val time = measureTimeMillis { val location = channelLocation(ws) val weather = channelWeather(location, ws) weather.consumeEach { w -> log("got weather : $w") } location.cancel() weather.cancel() } log("\ndone in $time") } private fun channelWeather(locationChannel: ProducerJob<Location>, ws: BlockingWeatherWS) = produce<Forecastday_>(CommonPool) { log("get weather for $locationChannel") locationChannel.consumeEach { location -> val list = ws.weather(location.lat, location.lng, "EN").execute().body().forecast?.simpleforecast?.forecastday?.take(4).orEmpty() list.forEach { send(it) } } } private fun channelLocation(ws: BlockingWeatherWS) = produce<Location>(CommonPool) { log("get location") val location = ws.geocode("Toulouse,fr").execute().body().results.first().geometry?.location location?.let { send(location) } } -
@Test fun testWeather() = runBlocking { val ws = retrofitWS("https://my-weather-api.herokuapp.com/")
val time = measureTimeMillis { val location = channelLocation(ws) val weather = channelWeather(location, ws) weather.consumeEach { w -> log("got weather : $w") } location.cancel() weather.cancel() } log("\ndone in $time") } private fun channelWeather(locationChannel: ProducerJob<Location>, ws: BlockingWeatherWS) = produce<Forecastday_>(CommonPool) { log("get weather for $locationChannel") locationChannel.consumeEach { location -> val list = ws.weather(location.lat, location.lng, "EN").execute().body().forecast?.simpleforecast?.forecastday?.take(4).orEmpty() list.forEach { send(it) } } } private fun channelLocation(ws: BlockingWeatherWS) = produce<Location>(CommonPool) { log("get location") val location = ws.geocode("Toulouse,fr").execute().body().results.first().geometry?.location location?.let { send(location) } } -
@Test fun testWeather() = runBlocking { val ws = retrofitWS("https://my-weather-api.herokuapp.com/")
val time = measureTimeMillis { val location = channelLocation(ws) val weather = channelWeather(location, ws) weather.consumeEach { w -> log("got weather : $w") } location.cancel() weather.cancel() } log("\ndone in $time") } private fun channelWeather(locationChannel: ProducerJob<Location>, ws: BlockingWeatherWS) = produce<Forecastday_>(CommonPool) { log("get weather for $locationChannel") locationChannel.consumeEach { location -> val list = ws.weather(location.lat, location.lng, "EN").execute().body().forecast?.simpleforecast?.forecastday?.take(4).orEmpty() list.forEach { send(it) } } } private fun channelLocation(ws: BlockingWeatherWS) = produce<Location>(CommonPool) { log("get location") val location = ws.geocode("Toulouse,fr").execute().body().results.first().geometry?.location location?.let { send(location) } } -
@Test fun testWeather() = runBlocking { val ws = retrofitWS("https://my-weather-api.herokuapp.com/")
val time = measureTimeMillis { val location = channelLocation(ws) val weather = channelWeather(location, ws) weather.consumeEach { w -> log("got weather : $w") } location.cancel() weather.cancel() } log("\ndone in $time") } private fun channelWeather(locationChannel: ProducerJob<Location>, ws: BlockingWeatherWS) = produce<Forecastday_>(CommonPool) { log("get weather for $locationChannel") locationChannel.consumeEach { location -> val list = ws.weather(location.lat, location.lng, "EN").execute().body().forecast?.simpleforecast?.forecastday?.take(4).orEmpty() list.forEach { send(it) } } } private fun channelLocation(ws: BlockingWeatherWS) = produce<Location>(CommonPool) { log("get location") val location = ws.geocode("Toulouse,fr").execute().body().results.first().geometry?.location location?.let { send(location) } } -
@Test fun testWeather() = runBlocking { val ws = retrofitWS("https://my-weather-api.herokuapp.com/")
val time = measureTimeMillis { val location = channelLocation(ws) val weather = channelWeather(location, ws) weather.consumeEach { w -> log("got weather : $w") } location.cancel() weather.cancel() } log("\ndone in $time") } private fun channelWeather(locationChannel: ProducerJob<Location>, ws: BlockingWeatherWS) = produce<Forecastday_>(CommonPool) { log("get weather for $locationChannel") locationChannel.consumeEach { location -> val list = ws.weather(location.lat, location.lng, "EN").execute().body().forecast?.simpleforecast?.forecastday?.take(4).orEmpty() list.forEach { send(it) } } } private fun channelLocation(ws: BlockingWeatherWS) = produce<Location>(CommonPool) { log("get location") val location = ws.geocode("Toulouse,fr").execute().body().results.first().geometry?.location location?.let { send(location) } } -
@Test fun testWeather() = runBlocking { val ws = retrofitWS("https://my-weather-api.herokuapp.com/")
val time = measureTimeMillis { val location = channelLocation(ws) val weather = channelWeather(location, ws) weather.consumeEach { w -> log("got weather : $w") } location.cancel() weather.cancel() } log("\ndone in $time") } private fun channelWeather(locationChannel: ProducerJob<Location>, ws: BlockingWeatherWS) = produce<Forecastday_>(CommonPool) { log("get weather for $locationChannel") locationChannel.consumeEach { location -> val list = ws.weather(location.lat, location.lng, "EN").execute().body().forecast?.simpleforecast?.forecastday?.take(4).orEmpty() list.forEach { send(it) } } } private fun channelLocation(ws: BlockingWeatherWS) = produce<Location>(CommonPool) { log("get location") val location = ws.geocode("Toulouse,fr").execute().body().results.first().geometry?.location location?.let { send(location) } } -
@Test fun testWeather() = runBlocking { val ws = retrofitWS("https://my-weather-api.herokuapp.com/")
val time = measureTimeMillis { val location = channelLocation(ws) val weather = channelWeather(location, ws) weather.consumeEach { w -> log("got weather : $w") } location.cancel() weather.cancel() } log("\ndone in $time") } private fun channelWeather(locationChannel: ProducerJob<Location>, ws: BlockingWeatherWS) = produce<Forecastday_>(CommonPool) { log("get weather for $locationChannel") locationChannel.consumeEach { location -> val list = ws.weather(location.lat, location.lng, "EN").execute().body().forecast?.simpleforecast?.forecastday?.take(4).orEmpty() list.forEach { send(it) } } } private fun channelLocation(ws: BlockingWeatherWS) = produce<Location>(CommonPool) { log("get location") val location = ws.geocode("Toulouse,fr").execute().body().results.first().geometry?.location location?.let { send(location) } } -
@Test fun testWeather() = runBlocking { val ws = retrofitWS("https://my-weather-api.herokuapp.com/")
val time = measureTimeMillis { val location = channelLocation(ws) val weather = channelWeather(location, ws) weather.consumeEach { w -> log("got weather : $w") } location.cancel() weather.cancel() } log("\ndone in $time") } private fun channelWeather(locationChannel: ProducerJob<Location>, ws: BlockingWeatherWS) = produce<Forecastday_>(CommonPool) { log("get weather for $locationChannel") locationChannel.consumeEach { location -> val list = ws.weather(location.lat, location.lng, "EN").execute().body().forecast?.simpleforecast?.forecastday?.take(4).orEmpty() list.forEach { send(it) } } } private fun channelLocation(ws: BlockingWeatherWS) = produce<Location>(CommonPool) { log("get location") val location = ws.geocode("Toulouse,fr").execute().body().results.first().geometry?.location location?.let { send(location) } } -
Coroutines - builder (runBlocking, async, launch …) - primitives (delay,
measureTime, job…) - communication (deferred, channel, selector, actor …) => Reactive world (RxJava …) => UI (JavaFX, Android …) => Testing (?) -
Further reading https:/ /github.com/Kotlin/kotlinx.coroutines/blob/master/coroutines-guide.md https:/ /github.com/Kotlin/kotlinx.coroutines/blob/master/ui/coroutines-guide-ui.md https:/ /github.com/Kotlin/anko/wiki/Anko-Coroutines https:/ /akarnokd.blogspot.fr/2017/09/rxjava-vs-kotlin-coroutines-quick-look.html?m=1
https:/ /github.com/Kotlin/kotlinx.coroutines/blob/master/reactive/coroutines- guide-reactive.md Coroutines vs RxJava -
Others stuffs in 1.1 https:/ /kotlinlang.org/docs/reference/whatsnew11.html Gradle Kotlin Scripts https:/
/github.com/gradle/kotlin-dsl Kotlin Native https:/ /github.com/JetBrains/kotlin-native