zla*_*aja 6 annotations scala metaprogramming dotty scala-macros
是否可以在 Dotty, Scala 3 中生成一个带有宏的新类?
兹拉亚
小智 7
您可以创建一个返回结构类型的透明宏,以生成具有您希望的任何s 和s 的类型。valdef
这是一个例子;method props,当使用 Product 类型调用时,会创建一个对象,其中第一个 Product 元素名称为 String val。
case class User(firstName: String, age: Int)
// has the type of Props { val firstName: String }
val userProps = props[User]
println(userProps.firstName) // prints "prop for firstName"
println(userProps.lastName) // compile error
Run Code Online (Sandbox Code Playgroud)
以及实现,有点棘手,但还不错:
import scala.compiletime.*
import scala.quoted.*
import scala.deriving.Mirror
class Props extends Selectable:
def selectDynamic(name: String): Any =
"prop for " + name
transparent inline def props[T] =
${ propsImpl[T] }
private def propsImpl[T: Type](using Quotes): Expr[Any] =
import quotes.reflect.*
Expr.summon[Mirror.ProductOf[T]].get match
case '{ $m: Mirror.ProductOf[T] {type MirroredElemLabels = mels; type MirroredElemTypes = mets } } =>
Type.of[mels] match
case '[mel *: melTail] =>
val label = Type.valueOfConstant[mel].get.toString
Refinement(TypeRepr.of[Props], label, TypeRepr.of[String]).asType match
case '[tpe] =>
val res = '{
val p = Props()
p.asInstanceOf[tpe]
}
println(res.show)
res
Run Code Online (Sandbox Code Playgroud)
使用递归,您可以细化 Refinement(因为 Refinement <: TypeRepr),直到所有内容都构建完成。
话虽如此,使用transparent inline甚至 Scala 2 宏注释来生成新类型都会使 IDE 很难支持自动完成。因此,如果可能的话,我建议使用标准Typeclass 派生。
您甚至可以派生标准特征的默认行为:
trait SpringDataRepository[E, Id]:
def findAll(): Seq[E]
trait DerivedSpringDataRepository[E: Mirror.ProductOf, Id]:
def findAll(): Seq[E] = findAllDefault[E, Id]()
private inline def findAllDefault[E, Id](using m: Mirror.ProductOf[E]): Seq[E] =
findAllDefaultImpl[E, m.MirroredLabel, m.MirroredElemLabels]()
private inline def findAllDefaultImpl[E, Ml, Mels](columns: ArrayBuffer[String] = ArrayBuffer()): Seq[E] =
inline erasedValue[Mels] match
case _: EmptyTuple =>
// base case
println("executing.. select " + columns.mkString(", ") + " from " + constValue[Ml])
Seq.empty[E]
case _: (mel *: melTail) =>
findAllDefaultImpl[E, Ml, melTail](columns += constValue[mel].toString)
Run Code Online (Sandbox Code Playgroud)
然后,用户所要做的就是扩展DerivedSpringDataRepository他们的产品类型:
trait SpringDataRepository[E, Id]:
def findAll(): Seq[E]
trait DerivedSpringDataRepository[E: Mirror.ProductOf, Id]:
def findAll(): Seq[E] = findAllDefault[E, Id]()
private inline def findAllDefault[E, Id](using m: Mirror.ProductOf[E]): Seq[E] =
findAllDefaultImpl[E, m.MirroredLabel, m.MirroredElemLabels]()
private inline def findAllDefaultImpl[E, Ml, Mels](columns: ArrayBuffer[String] = ArrayBuffer()): Seq[E] =
inline erasedValue[Mels] match
case _: EmptyTuple =>
// base case
println("executing.. select " + columns.mkString(", ") + " from " + constValue[Ml])
Seq.empty[E]
case _: (mel *: melTail) =>
findAllDefaultImpl[E, Ml, melTail](columns += constValue[mel].toString)
Run Code Online (Sandbox Code Playgroud)
目前在 Dotty 中只有(某种)def macros。目前没有(某种)宏注释,可以生成新成员、新类等。
对于生成新成员、新类等,您可以使用
让我提醒您,即使在 Scalac 中,生成新成员、新类等的能力也不是从一开始就出现的。此类功能(宏注释)作为Scalac 的Macro Paradise编译器插件出现。
我不能排除某个时候有人会为 Dotty 写一些类似 Macro Paradise 的东西。现在还为时过早,现在只是 Dotty 的功能冻结,甚至语言语法(例如)和标准库现在也在不断变化(还有一些库正在测试它们与 Dotty 一起工作的能力,例如目前没有 Scalaz /猫在那里)。