Open KotlinIsland opened 4 years ago
declare function foo(): string | number
It's pretty messy, but this could be represented with:
external fun foo(): FooResult /* String | Number */ external interface FooResult @ExperimentalContracts @Suppress("INCOMPATIBLE_TYPES") inline fun FooResult.isString(): Boolean { contract { returns(true) implies(this@isString is String) } return this.asDynamic() is String } @ExperimentalContracts @Suppress("INCOMPATIBLE_TYPES") inline fun FooResult.isNumber(): Boolean { contract { returns(true) implies(this@isNumber is Number) } return this.asDynamic() is Number } @ExperimentalContracts fun main() { val f = foo() when { f.isString() -> f.length f.isNumber() -> f.toInt() + 1 } }
It's pretty messy, but this could be represented with: