Scala 3 Macro Tutorial

Scala 3 Macro Tutorial

  • Tutorial
  • Contribute
  • GitHub

›Tutorial

Tutorial

  • Introduction
  • Inline
  • Scala Compile-time Operations
  • Scala 3 Macros
  • Quoted Code
  • TASTy Reflection

Extra

  • FAQ
  • Best Practices
  • Other Recources
Edit

TASTy Reflection

The reflection API provides a more complex and comprehensive view on the structure of the code. It provides a view on the Typed Abstract Syntax Trees TASTy and their properties such as types, symbols, positions and comments.

How to use the API

Accessing this API need and import that depends the current QuoteContext. We can use scala.quoted.qctx to import it.

def pow(x: Expr[Int])(using QuoteContext): Expr[Int] = {
  import qctx.tasty._ // Import Tree, Type, Symbol, Position, .....
  ...
}

This will import all the types and modules (with extension methods) of the API.

The full imported API can be found here: Reflection

For example to find what is a Term, we can see in the hierarchy that it is a subtype of Statement which is a subtype of Tree. If we look into the TermMethods we will find all the extension methods that are defined for Term such as Term.tpe which returns a Type. As it is a subtype of Tree we can also look into the TreeMethods to find more methods such as Tree.pos. Each type also a module with some static-ish methods, for example in the TypeModule we can find the method Type.of[T] with will create an instance of Type containing T.

Relation with expressions

*Coming soon*

Examples

Coming soon

← Quoted CodeFAQ →
  • How to use the API
  • Relation with expressions
  • Examples
Scala 3 Macro Tutorial
Docs
TutorialContributeFAQ
Community
Chat on GitterDiscuss on Scala Users
More
GitHub
Copyright © 2020 LAMP EPFL