Let us have the following enum definition.
enum Events: case First case Second case HanderReceived(value: Int)
When using this code, we can write
Second.ordinal but not
HandlerReceived.ordinal, because ordinal is a method of case instance.
Proposal: add an ordinal method to the companion objects of enums ADT to receive ability call method ordinal without instantiation of case instance.
It is helpful when we want to index something by possible ordinals.
Let us have some processing engine with those events and keep somewhere in our program
Map[Int, Event] for the last coming events.
var lastEvents: Map[Int, Event] = Map.empty
When we receive an event, we can update this map:
lastEvent = lastEvent.updated(event.ordinal, event)
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.