Programmatic structural types in Scala 3 have a lot of promise, especially as return values from macros (which cannot create classes via statements currently). With transparent inline
, it’s possible for a macro to generate a programmatic structural type based on user input, simulating class definition generation. However, the problem with transparent inline
is that the type cannot be knowable to the user (unless the user writes the type manually).
With tuples, Tuple.Map and Tuple.FlatMap allow us to create similar but different tuple definitions based on a source tuple definition. We could do the same with programmatic structural types and case classes, if not for the problem of the resulting type being unknowable unless the user manually defines it.
If structural types were able to be defined in a way that member names could be derived from a string literal (or even a literal of the ScalaName
type that is the set of string singletons that are valid names in Scala), it would be possible to define match types that could build up derivative structural types piece by piece based off a case class mirror.
Basically, I want the ability to do something like this:
type Derivative[T <: Tuple, F[_]] = T match
case (s & Singleton & String, v) *: tail => Selectable { val $s: F[v] } & Derivative[tail]
case EmptyTuple => Selectable
Is it forseeable that something like this could be added in Scala 3?
p.s. something like this is already achievable via Dynamic, it’s just that the compiler isn’t aware of the available members until a user tries to use them.