In the C word, enumerations are simply compile time symbols known by the compiler, not represented in any sort of memory allocated entity. On the Scala world, however, the developer may be interested on performing pattern matching with enumerations. In certain circumstances, a developer coming from the Java world will be eventually interested on comparing enumerated values, or obtaining the textual representation of a given enumerated value.
These are some (arguably silly!) use cases:
// use case: enums can participate in pattern matching
val isWeekend =
weekday match {
case Weekdays.Sat => true
case Weekdays.Sun => true
case _ => false
}
// use case: ability to obtain the textual name of an enumerated value
val today: Weekdays = ...
val happy = today.name.startsWith('F')
// use case: ability to obtain the cardinality of an enumerated value
val today: Weekdays = ...
val isWeekend = today.cardinal > 5
// use case: enumerations can be ordered. See: https://gist.github.com/frgomes/da098c2f10a08440f5de
val agenda: Map[Weekdays, Seq[Appointment]] = ...
agenda.sorted. foreach { ... }
In addition to these use cases, the user may be interested on smooth interoperability with Java enumerations, something that may be available in future versions of Scala. Regardless the current version of Scala we are running on or we are effectively supporting at this point, it would be handy if the user could today generate bindinds with future proof compatibility with upcoming features of future versions of the platform.
Proposal (this is subject to change)
The user can, on the command line, choose how enumerations are generated. For example:
Background
In the C word, enumerations are simply compile time symbols known by the compiler, not represented in any sort of memory allocated entity. On the Scala world, however, the developer may be interested on performing pattern matching with enumerations. In certain circumstances, a developer coming from the Java world will be eventually interested on comparing enumerated values, or obtaining the textual representation of a given enumerated value.
These are some (arguably silly!) use cases:
In addition to these use cases, the user may be interested on smooth interoperability with Java enumerations, something that may be available in future versions of Scala. Regardless the current version of Scala we are running on or we are effectively supporting at this point, it would be handy if the user could today generate bindinds with future proof compatibility with upcoming features of future versions of the platform.
Proposal (this is subject to change)
The user can, on the command line, choose how enumerations are generated. For example:
... meaning that enumeration
Weekdays
must follow a "c-like" style whilst enumerationStatus
must follow an "ordered" style.More on these styles below:
[ ] Simple C-like enumerations, assigning values explicitly.
[ ] Scala enumerations, not assigning values explicitly, no Java interoperability.
[ ] Employing objects of a sealed trait, no Java interoperability.
[ ] Employing objects of a sealed trait, supporting Java interoperability? (to be confirmed!)
[ ] Enumeratios which employ
Ordered
with cardinality and textual representation.