how do i create data objects in kotlin similarly t...
# announcements
w
how do i create data objects in kotlin similarly to what i could do with javascript:
Copy code
const myObject = {
  someKey: someValue,
  someOtherKey: [{foo: "bar"}, {bar: "foo"}]
}
c
You can create anonymous objects, but they are only available in the local scope (can’t pass them outside of the function they’re declared in) (https://kotlinlang.org/docs/reference/object-declarations.html#object-expressions). The only other way to get something like that is with the
mapOf()
function:
Copy code
val myObject = mapOf(
  "someKey" to someValue,
  "someOtherKey" to listOf(mapOf("foo" to "bar"), mapOf("bar" to "foo"))
)
👆 1
👍 2
m
Or you can use a full-blown
data class
of course.