Ravinder Payal
Ravinder Payal

Reputation: 3041

ScalaMeta: What's difference between `stats` and `children`

stats is short for statements while children is what it's. I ran both methods and it seems results are same. Can someone please point to me a more detailed differentiation between both.

Attaching a sample code, feel free to run it on 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")
})

Upvotes: 0

Views: 68

Answers (1)

Dmytro Mitin
Dmytro Mitin

Reputation: 51703

Every Tree has children: List[Tree] but only Source (and its inheritors) has 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

Upvotes: 0

Related Questions