spierce7
01/19/2018, 4:50 PMPlatform.select({
ios: 'iOS',
android: 'android',
});
anton.bannykh
01/19/2018, 5:10 PMkotlin
external interface Config {
val ios: String
val android: String
}
external object Platform {
fun select(config: Config)
}
spierce7
01/19/2018, 5:58 PMspierce7
01/19/2018, 5:59 PMspierce7
01/19/2018, 6:31 PMspierce7
01/20/2018, 2:49 AMfun <T> Platform.select(ios: T, android: T): T {
@Suppress("UnsafeCastFromDynamic")
return select(
jsObject {
this.ios = ios
this.android = android
}
)
}
inline fun jsObject(init: dynamic.() -> Unit): dynamic {
val o = jsObject()
init(o)
return o
}
fun jsObject(): dynamic = js("{}")
gildor
01/22/2018, 2:56 AMjson(
"ios" to "something",
"android" to "something"
)
spierce7
01/22/2018, 7:14 AMgildor
01/22/2018, 7:24 AManton.bannykh
01/22/2018, 10:31 AMConfig
and use that. Yes, that would be suboptimal performace-wise
In my opinion a simpler way to remedy that would be creating a factory function, which looks like a constructor:
fun Config(ios: String, android: String): Config {
val res = js("{}")
res.ios = ios
res.android = android
return res.unsafeCast<Config>()
}
Calling select looks like this: Platform.select(Config("ios", "android"))
. This way you won't need a special extension function for every usage of Config
.