在 GitHub 上編輯此頁面

TASTy 檢查

TASTy 檔案包含類別的完整已鍵入樹狀結構,包括來源位置和文件。這對於分析或從程式碼中擷取語義資訊的工具來說是理想的。

為避免直接使用 TASTy 檔案的麻煩,我們提供載入內容並透過 TASTy 反射 API 公開內容的 Inspector

我們還展示了 TASTyViz,一個用於視覺化 tasty 的視覺化工具,有助於除錯和檢查您對 TASTy 的理解

TASTyViz

TASTyViz 是用於視覺化檢查 TASTy 檔案的工具。在撰寫本文時,它仍處於開發的早期階段,因此您可能會遇到功能缺失和不太理想的使用者體驗,但它在除錯時仍可能證明是有用的。您可以在 這裡查看它。

Inspector

Inspector 是一個提供 API 存取 TASTy 的工具。

您可以像這樣將依賴項新增到您的 sbt 建構中

libraryDependencies += "org.scala-lang" %% "scala3-tasty-inspector" % scalaVersion.value

若要檢查 TASTy 檔案的樹狀結構,消費者可以以下列方式定義。

import scala.quoted.*
import scala.tasty.inspector.*

class MyInspector extends Inspector:
   def inspect(using Quotes)(tastys: List[Tasty[quotes.type]]): Unit =
      import quotes.reflect.*
      for tasty <- tastys do
         val tree = tasty.ast
         // Do something with the tree

然後消費者可以使用以下程式碼實例化來取得 foo/Bar.tasty 檔案的樹狀結構。

object Test:
   def main(args: Array[String]): Unit =
      val tastyFiles = List("foo/Bar.tasty")
      TastyInspector.inspectTastyFiles(tastyFiles)(new MyInspector)

請注意,如果我們需要在編譯後執行主程式 (在以下範例中定義在稱為 Test 的物件中),我們需要讓編譯器可供執行時期使用

scalac -d out Test.scala
scala -with-compiler -classpath out Test

範本專案

使用 sbt 版本 1.1.5+,執行

sbt new scala/scala3-tasty-inspector.g8

在您要複製範本的資料夾中。