inline fun <reified T : Enum<T>> List<String>.toEnums(): List<T> {
return this.map {
enumValueOf<T>(it)
}
}
a
andries.fc
03/09/2018, 8:42 PM
Here is my solution (possibly not the most optimal):
inline fun <reified T:Enum<T>> List<String>.toEnums(): List<T> = T::class.java.enumConstants.filter { it.name in this }.toList()
andries.fc
03/09/2018, 8:48 PM
Here is a full example:
`
fun main(args: Array<String>) {
val widgets = listOf("TOGGLE","BUTTON").toEnums<WidgetType>().toSet()
val expected = setOf(WidgetType.TOGGLE, WidgetType.BUTTON)
assertTrue { expected == widgets }
}
'
c
chan
06/24/2019, 8:18 AM
Just seeing this now. It’s a bit late, but thanks!