简体   繁体   中英

Scala Reflection why NoPosition on reify tree?

The scala.reflect.api.Position and the documentation Annotations, Names, Scopes, and More don't give enough explanation about why trees or symbols could have a position as NoPosition.

In the code below, what is the reason the symbols, terms, constant all have NoPosition?

import scala.reflect.runtime.universe._

def movieTree = reify {
  final class Movie {
    val year = 1998
    def name = "You've Got Mail"
  }
}.tree

showRaw(movieTree, printPositions = true)

// res0: String = [NoPosition]Block(
// List([NoPosition]ClassDef(Modifiers(FINAL), TypeName("Movie"), List(),
// [NoPosition]Template(List([NoPosition]Ident(TypeName("AnyRef"))), noSelfType,
// List([NoPosition]DefDef(Modifiers(), termNames.CONSTRUCTOR, List(), List(List()),
// [NoPosition]TypeTree(), 
// [NoPosition]Block(List([NoPosition]Apply([NoPosition]Select([NoPosition]Super([NoPosition]This(typeNames.EMPTY), typeNames.EMPTY), 
// termNames.CONSTRUCTOR), List())), [NoPosition]Literal(Constant(())))), 
// [NoPosition]ValDef(Modifiers(), TermName("year"), [NoPosition]TypeTree(), [NoPosition]Literal(Constant(1998))),
// [NoPosition]DefDef(Modifiers(), TermName("name"), List(), List(), [NoPosition]TypeTree(),
// [NoPosition]Literal(Constant("You've Got Mail"))))))),
// [NoPosition]Literal(Constant(())))

Every non-empty position refers to a SourceFile and three character offsets within it: start, end, and point.

So if you don't get a tree by parsing some SourceFile , it doesn't have any positions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM