But we can’t look - are we receive HandlerRecieved:
lastEvent.get(HandlerReceived.ordinal) // error now: value ordinal is not a member of Event.HandlerReceived , but could be made available as an extension method.
Adding ordinal alias to Event.HandlerReceived will make the situation more pleasant.
You can already implement this functionality with the help of scala.deriving.Mirror, which can reflect on the ordinals of enum types.
In 3.0.2 you should be able to write the following:
import compiletime.ops.int.+
import compiletime.constValue
import deriving.Mirror
inline def ordinalOf[E, C <: E](using me: Mirror.SumOf[E]): Int =
constValue[SearchElem[me.MirroredElemTypes, C, 0]]
type SearchElem[T <: Tuple, X, N <: Int] <: Int = T match
case X *: _ => N
case _ *: xs => SearchElem[xs, X, N + 1]
and here i use it with Events:
enum Events:
case First
case Second
case HanderReceived(value: Int)
val ordFirst: Int = ordinalOf[Events, Events.First.type]
val ordSecond: Int = ordinalOf[Events, Events.Second.type]
val ordHandlerRecieved: Int = ordinalOf[Events, Events.HanderReceived]
This could be improved if we could have a way to summon the mirror of the parent of an enum case, so you would only need to pass Events.HandlerRecieved
Thanks, … from the usability point of view, I still expect to have original in ADT companion object will be practically worth having. [ why – I accidentally wrote a block of code, thinking that this functionality exists. I guess that my application programmer intuition is more or less the same as average, so others will be come to the same logic]. So, feature is ‘nice to have’.