ankushg
06/27/2019, 7:49 PMdata class Foo(val bar: Int)
as a parameter, I can call it from JS and pass in the blob {"bar": 1}
and it will Just Work.
Is this officially supported, or is it just an unreliable side-effect of how Kotlin/JS internally represents data classes?
Does this work with nested classes (e.g., data class Foo2(val myBar: Bar)
and data class Bar(...)
)?
If I had a class that was declared as data class Foo3(val bar: SomeEnumClass)
, is there a simple way to allow me to call it from JS using a blob with an Int
map it to SomeEnumClass
in Kotlin? Maybe something like an alternate constructor or property with a backing fields?Roman Artemev [JB]
06/27/2019, 8:17 PMvalues()
which returns an array of enum entries so it works like this
enum class E { Foo, Bar }
E.values()[0] // Foo
E.values()[1] // Bar
ankushg
06/27/2019, 8:32 PMkotlin
data class Input(val inputType: InputType)
enum class InputType { Foo, Bar }
fun processInput(input: Input) = TODO("somehow process the input")
This does not work in Javascript:
javascript
const input = { "inputType": 1 }
processInput(input)
If I define these additional intermediate classes/methods in my Kotlin/JS platform code:
kotlin
data class InputJs(val inputType: Int) {
fun toCommmon() = Input(InputType.values()[inputType])
}
fun processInputJs(input: InputJs) = procesInput(input.toCommon())
Then I can call
javascript
const input = { "inputType": 1 }
processInputJs(input)
Is there a way to do this without having to define extra classes/methods?ankushg
06/27/2019, 8:34 PMRoman Artemev [JB]
06/28/2019, 9:16 AMankushg
06/28/2019, 2:29 PMRoman Artemev [JB]
06/29/2019, 8:48 AM