再会!我正在尝试在 Scala 2.9.1 中构建不可变图。它是给我的Seq[BO],其中BO可以代表图中的一个节点,而BO.attr_bo: Seq[String]who 代表其他节点的边,由字符串名称给出。我需要构建“已解析”图,由BO with ResolvedBO
您可以在此处看到可能的实现:
trait BO {
def name: String
def attr_bo: Seq[String]
}
trait ResolvedBO {
x: BO =>
val uni: Universe
lazy val r_attr_bo: Seq[BO with ResolvedBO] = attr_bo map (uni.m_list_r(_))
}
class S_BO(val name: String, val attr_bo: Seq[String]) extends BO
class Universe(list: Seq[BO]) {
val m_list: Map[String, BO] = list.map(x => (x.name, x))(collection.breakOut)
val m_list_r: Map[String, BO with ResolvedBO] = ...???
}
val x: Uni …Run Code Online (Sandbox Code Playgroud) 我有下一个类结构:
trait SomeType
trait Root {
val allMySomeTypes: Seq[SomeType]
}
class Child extends Root {
object MyType1 extends SomeType {...}
object MyType2 extends SomeType {...}
}
Run Code Online (Sandbox Code Playgroud)
我想初始化val allMySomeTypes作为扩展在具体类中定义的SomeType的所有对象的Seq.所以对于Child实例,它将是val allMySomeTypes:Seq [SomeType] = Seq(MyType1,MyType2).
我写了一个宏来查找具有一些基类型的对象:
def getMembersOfCurrentByType_impl[S](c: Context)(implicit ev: c.WeakTypeTag[S]) = {
import c.universe._
val seqApply = Select(reify(Seq).tree, newTermName("apply"))
val objs = c.enclosingClass.symbol.typeSignature.declarations.collect {
case o: ModuleSymbol if o.typeSignature <:< ev.tpe => Ident(o) //Select(c.prefix.tree, o)
}.toList
c.Expr[Seq[S]] {Apply(seqApply, objs)}
}
Run Code Online (Sandbox Code Playgroud)
并绑定到
trait Root {
val allMySomeTypes: Seq[SomeType] = macro getMembersOfCurrentByType_impl[SomeType]
}
Run Code Online (Sandbox Code Playgroud)
但是,显然,由于基本特征的宏扩展,我在Child类中有空序列.我可以在没有在Child类中额外输入而不使用运行时反射的情况下构建实际成员的Seq吗?