https://kotlinlang.org logo
#announcements
Title
# announcements
a

Andrew Gazelka

07/02/2019, 9:43 PM
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

karelpeeters

07/02/2019, 9:52 PM
The spread operator
*
only works on arrays, not sets.
t

Toddobryan

07/02/2019, 10:13 PM
Yeah. So you'd need to
.toTypedArray()
first.
k

karelpeeters

07/02/2019, 10:14 PM
You'd need that instead.
t

Toddobryan

07/02/2019, 10:16 PM
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

karelpeeters

07/02/2019, 10:17 PM
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

Hullaballoonatic

07/04/2019, 8:07 PM
makes me wonder if it would be a good idea to make the spread operator overloadable
k

karelpeeters

07/05/2019, 8:08 AM
h

Hullaballoonatic

07/05/2019, 11:14 PM
yikes, the thought on adding
operator fun spread
is 3 years old. probably not gonna happen at this point