I have a lot of enum class in my Kotlin application. They all represent bit flags that I need to convert between EnumSet<> and UInt for serial communication with an FPGA board.
How can I provide some extension methods that apply to all those Enums?
Enums don't allow class inheritance. EnumSet can't reference an interface. I'm not sure what else to try.
Thanks!
enum class ReadFlag(val bits: UInt) {
DATA_READY(1u)
BUSY(2u)
}
typealias ReadFlags = EnumSet<ReadFlag>
enum class WriteFlag(val bits: UInt) {
UART_RESET(1u)
BUSY(2u)
PAGE_DATA(4u)
}
typealias WriteFlags = EnumSet<WriteFlag>
fun ReadFlags.asUInt(): UInt =
this.fold(0u) { acc, next -> acc or next.bits }
fun WriteFlags.asUInt(): UInt =
this.fold(0u) { acc, next -> acc or next.bits }
This results in this error:
error: platform declaration clash: The following declarations have the same JVM signature (asUInt(Ljava/util/EnumSet;)I):
Write an interface for the common members:
Implement the interface:
Then you can make your
asUIntgeneric: