我正在使用 scala3 进行编码,利用编程结构类型。结构类型恰好模仿了现有的案例类:它们的定义是纯粹的样板,因此很容易通过元编程来制作它们。
我了解如何制作函数实现,通常是通过类型类派生。但在这里我们正在尝试制作一个(结构)类型。
这在 scala2 中是可能的,通过类宏注释,但这些在 scala3 中已经消失了。有办法吗?如果是这样怎么办?
下面的代码是我想获得的结果:
// Library part
trait View extends scala.Selectable :
def selectDynamic(key:String) =
println(s"$key is being looked up")
???
// DSL Definition part
case class SomeDefWithInt ( i : Int )
case class SomeDefWithString( s : String )
// Boiler-plate code
type ViewOf[M] = M match
case SomeDefWithInt => View { def i : Int }
case SomeDefWithString => View { def s : …Run Code Online (Sandbox Code Playgroud)