Anyone have any idea why this isn’t working? (`val...
# announcements
a
Anyone have any idea why this isn’t working? (
val people = setOf(personA, personC)
)
Copy code
json {
        "test1" to array(personA, personC) //works
        "test2" to array(*people) // does not work
    }
Copy code
interface KlaxonJson {
fun array(vararg args: Any?) : JsonArray<Any?> = JsonArray(args.map { convert(it) })
// ...
k
The spread operator
*
only works on arrays, not sets.
t
Yeah. So you'd need to
.toTypedArray()
first.
k
You'd need that instead.
t
I meant that you need to call
.toTypedArray()
before you use the spread operator, but you'd end up with
(*people.toTypedArray())
and I think the precedence works out so that the set is converted to an array before the spread operator is applied. (Sorry for using "first" in a horribly ambiguous way.)
k
Yeah I mean that if you want to convert a set to an array you don't need
*
and
toTypedArray
, just the latter will do simple smile
h
makes me wonder if it would be a good idea to make the spread operator overloadable
k
h
yikes, the thought on adding
operator fun spread
is 3 years old. probably not gonna happen at this point