2014-10-13 227 views
0

在運行時獲取指定變量的名稱讓一類在斯卡拉

case class C(val i: Int) { 
    var assignee: Option[String] = None 
} 

和給定的分配給一個名爲x變量,

val x = C(1) 
x: C = C(1) 

如何獲得價值"x": Stringx.assignee,即

assert(x.assignee == Some("x")) 
+0

如果這個對象是在數組或某些集合? –

+0

@DenisTulskiy好點,只有'val'分配,否則'受讓人'仍然是'None'。 – elm

+0

不知道你在問什麼。 – tuxdna

回答

1

本地變量名在運行時不可用。但您可以在編譯時使用a macro來捕獲它。見例如definingValNamehttps://github.com/sbt/sbt/blob/9c442d3aed53bdc89db1ada9d5b204bf02adb339/main/settings/src/main/scala/sbt/std/KeyMacro.scala和它的用法:

def definingValName(c: Context, invalidEnclosingTree: String => String): String = { 
    import c.universe.{ Apply => ApplyTree, _ } 
    val methodName = c.macroApplication.symbol.name 
    def processName(n: Name): String = n.decoded.trim // trim is not strictly correct, but macros don't expose the API necessary 
    def enclosingVal(trees: List[c.Tree]): String = { 
    trees match { 
     case vd @ ValDef(_, name, _, _) :: ts => processName(name) 
     case (_: ApplyTree | _: Select | _: TypeApply) :: xs => enclosingVal(xs) 
     // lazy val x: X = <methodName> has this form for some reason (only when the explicit type is present, though) 
     case Block(_, _) :: DefDef(mods, name, _, _, _, _) :: xs if mods.hasFlag(Flag.LAZY) => processName(name) 
     case _ => 
     c.error(c.enclosingPosition, invalidEnclosingTree(methodName.decoded)) 
     "<error>" 
    } 
    } 
    enclosingVal(enclosingTrees(c).toList) 
} 

def enclosingTrees(c: Context): Seq[c.Tree] = 
    c.asInstanceOf[reflect.macros.runtime.Context].callsiteTyper.context.enclosingContextChain.map(_.tree.asInstanceOf[c.Tree]) 
+0

非常感謝,如何編譯它(無法進入REPL而沒有錯誤),以及如何運行它。 – elm