0

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")
})
4

1 回答 1

0

每个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

于 2021-03-25T23:00:16.340 回答