frgomes / scala-bindgen

A native binding generator for the Scala language.
BSD 3-Clause "New" or "Revised" License
34 stars 6 forks source link

Proposal for binding enumerations #16

Open frgomes opened 7 years ago

frgomes commented 7 years ago

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:

// 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:

scala-bindgen -e c-like:Weekdays -e ordered:Status ...

... meaning that enumeration Weekdays must follow a "c-like" style whilst enumeration Status must follow an "ordered" style.

More on these styles below:

frgomes commented 7 years ago

@jonas ideas?

frgomes commented 6 years ago

Add support for Enumeratum?