Чтение данных в массив numpy из текстового файла

Локальные имена переменных недоступны во время выполнения. Но вы можете захватить его с помощью макроса во время компиляции. См. definingValName в https://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 =  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))
        ""
    }
  }
  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])

13
задан Nirvan 26 November 2013 в 05:21
поделиться