fun serialize(aList: List<Any>)

When I call serialize with a List containing Symbol and Keyword, the compiler complains

type mismatch:

Require List<Any>
Found: List<Comparable<{Symbol & Keyword}>?>

Shouldn't List be covariant?

how can I construct a type signature to match what the compiler expects List<Comparable<{Symbol & Keyword}>?>

The compiler expects the type List<Comparable<{Symbol & Keyword}>?> but that is not valid kotlin syntax

1

There are 1 best solutions below

0
On

You have to make serialize() to accept List<Any?> instead since your Comparable type is nullable.

fun serialize(aList: List<Any?>) {}

If you want it to accept List<Comparable<{Symbol & Keyword}>?>, you have to make use of where-clause to provide multiple upper bounds:

fun <T> serialize(aList: List<Comparable<T>?>) where T: Symbol, T: Keyword {}