Hello, I have published a very simple library: enum-extensions, which provides EnumMirror[E]
, a type class that reflects a Scala 3 enumeration type E
.
At the base level the intention is to reflect all the abilities of the companion object, but it also provides exception-free lookup, cached values
, and name
/ordinal
extension methods to E
.
Use cases: defining generic code that works on all enumeration types, decoding cases dynamically (e.g. reading from file/command line), deriving other type classes.
e.g. I can define a “Mode” enum for a command-line app, and derive a FromString type class for use with @main
methods:
import enumextensions.EnumMirror
import scala.util.CommandLineParser
enum Mode derives EnumMirror:
case r, rw // read, readwrite
given [E](using EnumMirror[E]): CommandLineParser.FromString[E] =
EnumMirror[E].valueOfUnsafe(_)
@main def cliApp(mode: Mode) =
if mode == Mode.r then println("read mode")
else if mode == Mode.rw then println("readwrite mode")
I’m looking for more suggestions of utilities that would be useful to include.