I'm wondering if it is possible to write a macro in Scala 3 that take a set of strings and turn it into an enum type with with those strings as cases?
For example, I would like to write a class with an internal type generated from the input element:
import scala.quoted.*
class Example(myEnumElements:Seq[String]) {
inline def buildEnum(inline elts:Seq[String]): Unit = ${ buildEnumType('elts) }
def buildEnumType(e: Expr[Seq[String]])(using Quotes, Type[Seq]): Expr[Unit] = '{
enum MyEnum:
???
}
}
...
// Possibly in another file?
val example = Example(Seq("A","B","C"))
def someConvenienceFunction(e:example.MyEnum) = e match
case A => "apple"
case B => "banana"
case C => "cranberry"
...
// Possibly in another file?
someConvenienceFunction(example.A) // "apple"
someConvenienceFunction(example.D) // compile error