Skip to content

Instantly share code, notes, and snippets.

@nosix
Last active September 5, 2017 03:07
Show Gist options
  • Save nosix/538b6bf673547f723cb547c83033fd54 to your computer and use it in GitHub Desktop.
Save nosix/538b6bf673547f723cb547c83033fd54 to your computer and use it in GitHub Desktop.
union type in Kotlin/JS
function sample$lambda() {
return 'Bar';
}
function sample() {
var v1 = 'Foo';
var v2 = sample$lambda;
}
external interface ObjectOrFactory<T>
inline fun <T> ObjectOrFactory(value: T): ObjectOrFactory<T> = value.asDynamic()
inline fun <T> ObjectOrFactory(value: () -> T): ObjectOrFactory<T> = value.asDynamic()
inline fun <T> ObjectOrFactory.toObject(): T = this.asDynamic()
inline fun <T> ObjectOrFactory.toFactory(): () -> T = this.asDynamic()
fun sample() {
val v1: ObjectOrFactory<String> = ObjectOrFactory("Foo")
val v2: ObjectOrFactory<String> = ObjectOrFactory { "Bar" }
// val v3: ObjectOrFactory<String> = "Buz" // error
// val v4: ObjectOrFactory<String> = ObjectOrFactory(100) // error
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment