Consider the following (broken) code:
def makeClass[T: Type](using q: Quotes): Expr[T] = '{
class MyTest extends T { def foo: Int = 4 }
new MyTest
}
This fails with the following error:
A class type includes classes and traits in a specific order. Defining a class, even an anonymous class,
requires specifying a linearization order for the traits it extends. For example,A & B
is not a class type
because it doesn’t specify which trait takes precedence, A or B. For more information about class types, please see the Scala Language Specification.
Class types also can’t have refinements.
Is there any way (type contraint, implicit parameter, etc.) that I could provide evidence to the compiler that T
is a class or trait, and can be extended?
I’m aware of Symbol.newClass
which I think does what I want. But besides being less ergonomic than quoting, it is still experimental, and it seems that @experimental
is infections, and everything up the chain that uses the macro has to be experimental if I use it.
I also want to add any parameters on the primary constructor of T, if any, to the defined class, and pass them through, and I haven’t figured out how to do that yet.
1