ScalaMeta: В чем разница между `stats` и` children`
Прикрепив образец кода, смело запускайте его на Scastie ( https://scastie.scala-lang.org/A6huYabOTGmpZu9HRa6UZw )
val program = """
object Main {
def main(args: Array[String]): Unit = {
println("Hello Scalameta!")
}
def cats(args: Array[String]): Unit = {
println("Ancient cat here!")
}
def bats(args: Array[String]): Unit = {
println("Ancient bat here!")
}
def dragons = {
bats(List())
cats(List())
}
val ancient = dragons()
}
"""
import scala.meta._
val tree = program.parse[Source].get
println(tree.stats.head.children)
println("-----------------------\n")
tree.children.head.children.tail.head.children.foreach(child => {
println(s"${child.productPrefix} From: ${child.pos.startLine} and End: ${child.pos.endLine}")
println(child)
println("-----------------------\n")
})
1 ответ
Каждый
Tree
имеет
children: List[Tree]
но только
Source
(и его наследники)
stats: List[Stat]
.
https://www.javadoc.io/doc/org.scalameta/trees_2.13/latest/scala/meta/Tree.html
https://www.javadoc.io/doc/org.scalameta/trees_2.13/latest/scala/meta/Source.html