Pitel
08/19/2021, 11:33 AMenum class SomeHeaders(val label: String)
(for headers of different tables). How can I write a type parameter (like val a: ???WHAT???
) so that it will be clear that it's enum
(so I can use values()
, etc.) and it had the label
parameter. The parameter has easty solution: interface, but what about the enum
limitation?Roukanken
08/19/2021, 12:04 PMinterface Labeled {
val label: String
}
enum class LabeledEnum(override val label: String): Labeled {
THE_ONE("the one")
}
data class LabeledClass(override val label: String): Labeled
fun <T> test(a: T) where T: Enum<T>, T: Labeled {
println(a)
}
fun main() {
test(LabeledEnum.THE_ONE)
test(LabeledClass("not the one")) // this line does not compile
}
without generics it would need so called intersection types, usually written as Enum<T> & Labeled
or similarly, sadly Kotlin does not have these, nor there are any plans for them so farvalues()
you could use enumValues<T>()
but that would need to be inside reified inline functionPitel
08/19/2021, 12:44 PMwhere
keyword exactly for this 👍🏻test(LabeledEnum)
, like, pass the whole enum, not just single value.