Scala隐藏了奇怪的递归模板模式
所以-我在scala中使用的是java接口,它使用了奇怪的重复模板模式-它本质上是一个克隆函数:Scala隐藏了奇怪的递归模板模式,scala,scala-macros,Scala,Scala Macros,所以-我在scala中使用的是java接口,它使用了奇怪的重复模板模式-它本质上是一个克隆函数: interface Blah<T> { T clone(); } 但当然scala是相当神奇的——它有宏和其他东西——所以我想知道是否有可能(我想不出一个办法)将这种混乱隐藏在特性背后 比如说: trait BlahBetter extends Blah[this.type] { } 因此,任何使用它的人都不必知道他们是指他
interface Blah<T>
{
T clone();
}
但当然scala是相当神奇的——它有宏和其他东西——所以我想知道是否有可能(我想不出一个办法)将这种混乱隐藏在特性背后
比如说:
trait BlahBetter extends Blah[this.type] {
}
因此,任何使用它的人都不必知道他们是指他们自己-例如:
class LongNastyClass extends BlahBetter {
}
我会保留
类LongNastyClass扩展Blah[LongNastyClass]
(F-有界多态性)
但是可以引入宏注释
import scala.annotation.{StaticAnnotation, compileTimeOnly}
import scala.language.experimental.macros
import scala.reflect.macros.whitebox
@compileTimeOnly("enable macro paradise")
class blahBetter extends StaticAnnotation {
def macroTransform(annottees: Any*): Any = macro BlahBetterMacro.impl
}
object BlahBetterMacro {
def impl(c: whitebox.Context)(annottees: c.Tree*): c.Tree = {
import c.universe._
annottees match {
case q"$mods class $tpname[..$tparams] $ctorMods(...$paramss) extends { ..$earlydefns } with ..$parents { $self => ..$stats }" :: tail =>
val tparams1 = tparams.map {
case q"$_ type $tpname[..$_] = $_" => tpname
}
val parents1 = parents :+ tq"Blah[$tpname[..$tparams1]]"
q"""
$mods class $tpname[..$tparams] $ctorMods(...$paramss) extends { ..$earlydefns } with ..$parents1 { $self => ..$stats }
..$tail
"""
}
}
}
@blahBetter
class LongNastyClass
//Warning:scalac: {
// class LongNastyClass extends scala.AnyRef with Blah[LongNastyClass] {
// def <init>() = {
// super.<init>();
// ()
// }
// };
// ()
//}
import scala.annotation.{StaticAnnotation,compileTimeOnly}
导入scala.language.experimental.macros
导入scala.reflect.macros.whitebox
@compileTimeOnly(“启用宏天堂”)
类blahBetter扩展了StaticAnnotation{
def macroTransform(annottees:Any*):Any=macro BlahBetterMacro.impl
}
对象BlahBetterMacro{
def impl(c:whitebox.Context)(注释对象:c.Tree*):c.Tree={
导入c.universe_
注释匹配{
案例q“$mods class$tpname[…$tparams]$ctorMods(…$paramss)使用..$parents{$self=>..$stats}扩展了{..$earlydefns}”::tail=>
val tparams1=tparams.map{
案例q“$键入$tpname[..$\u]=$\ u”=>tpname
}
val parents1=父项:+tq“Blah[$tpname[..$tparams1].”
q”“”
$mods class$tpname[..$tparams]$ctorMods(..$paramss)使用..$parents1{$self=>..$stats}扩展了{..$earlydefns}
..$tail
"""
}
}
}
@喋喋不休
长类
//警告:scalac:{
//类LongNastClass使用Blah[LongNastClass]扩展scala.AnyRef{
//def()={
//超级(;
// ()
// }
// };
// ()
//}
this.type
不是LongNastyClass
我会保留类LongNastyClass扩展Blah[LongNastyClass]
(F-有界多态性)
但是可以引入宏注释
import scala.annotation.{StaticAnnotation, compileTimeOnly}
import scala.language.experimental.macros
import scala.reflect.macros.whitebox
@compileTimeOnly("enable macro paradise")
class blahBetter extends StaticAnnotation {
def macroTransform(annottees: Any*): Any = macro BlahBetterMacro.impl
}
object BlahBetterMacro {
def impl(c: whitebox.Context)(annottees: c.Tree*): c.Tree = {
import c.universe._
annottees match {
case q"$mods class $tpname[..$tparams] $ctorMods(...$paramss) extends { ..$earlydefns } with ..$parents { $self => ..$stats }" :: tail =>
val tparams1 = tparams.map {
case q"$_ type $tpname[..$_] = $_" => tpname
}
val parents1 = parents :+ tq"Blah[$tpname[..$tparams1]]"
q"""
$mods class $tpname[..$tparams] $ctorMods(...$paramss) extends { ..$earlydefns } with ..$parents1 { $self => ..$stats }
..$tail
"""
}
}
}
@blahBetter
class LongNastyClass
//Warning:scalac: {
// class LongNastyClass extends scala.AnyRef with Blah[LongNastyClass] {
// def <init>() = {
// super.<init>();
// ()
// }
// };
// ()
//}
import scala.annotation.{StaticAnnotation,compileTimeOnly}
导入scala.language.experimental.macros
导入scala.reflect.macros.whitebox
@compileTimeOnly(“启用宏天堂”)
类blahBetter扩展了StaticAnnotation{
def macroTransform(annottees:Any*):Any=macro BlahBetterMacro.impl
}
对象BlahBetterMacro{
def impl(c:whitebox.Context)(注释对象:c.Tree*):c.Tree={
导入c.universe_
注释匹配{
案例q“$mods class$tpname[…$tparams]$ctorMods(…$paramss)使用..$parents{$self=>..$stats}扩展了{..$earlydefns}”::tail=>
val tparams1=tparams.map{
案例q“$键入$tpname[..$\u]=$\ u”=>tpname
}
val parents1=父项:+tq“Blah[$tpname[..$tparams1].”
q”“”
$mods class$tpname[..$tparams]$ctorMods(..$paramss)使用..$parents1{$self=>..$stats}扩展了{..$earlydefns}
..$tail
"""
}
}
}
@喋喋不休
长类
//警告:scalac:{
//类LongNastClass使用Blah[LongNastClass]扩展scala.AnyRef{
//def()={
//超级(;
// ()
// }
// };
// ()
//}
此.type
不是LongNastClass