Spread, Type Inference and Array/List Issue?

Am I missing something obvious about what’s wrong with this call?

This works:

body("details[0].validChoices", Matchers.contains("A", "B"))

But this won’t:

body("details[0].validChoices", Matchers.contains(*listOf("A", "B")))

Interestingly, this will:

body("details[0].validChoices", Matchers.contains(*arrayOf("A", "B")))

So this will too:

val planNames = Plan.values().map(Plan::name).toTypedArray()
body("details[0].validChoices", Matchers.contains(*planNames))

Why do Array and List behave differently here?

AFAIK the spread operator is only defined for arrays, not lists or other types.

Ah, you know what, I think you’re right. This surprises me a bit, but it does at least explain the problem.

A slightly better error message would have helped, but at least I understand that what I attempted to do wasn’t valid. :wink:

Thanks for noticing! It’s really not an obvious error message. I created https://youtrack.jetbrains.com/issue/KT-30837, feel free to vote/follow for updates.

1 Like