Scala 3 Macro Tutorial

Scala 3 Macro Tutorial

  • Tutorial
  • Contribute
  • GitHub

›Extra

Tutorial

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

Extra

  • FAQ
  • Best Practices
  • Other Recources
Edit

FAQ

Which should I use Expr(...) or '{...}?

If you can write your code using Expr(...), you will evaluate more at compile time. Only use '{...} if you really need to evaluate the code later at runtime, usually because it depends on runtime values.

Which is better between Expr(true) or '{true}?

All quotes containing a value of a primitive type is optimised to an Expr.apply. Choose one in your project and stick with a single notation to avoid confusion.

How do I get a value out of an Expr?

If the expression represents a value, you can use .unlift, .unliftOrError, Unlifted.unapply or Const.unapply.

How can I get the precise type of an Expr?

We can get the precise type (Type) of an Expr using the following pattern match:

val x: Expr[X] = ...
x match
  case '{ $x: $t } =>
    // `x: Expr[X & t.T]` where `t` is the precise type of `x`
← TASTy ReflectionBest Practices →
  • Which should I use Expr(...) or '{...}?
  • Which is better between Expr(true) or '{true}?
  • How do I get a value out of an Expr?
  • How can I get the precise type of an Expr?
Scala 3 Macro Tutorial
Docs
TutorialContributeFAQ
Community
Chat on GitterDiscuss on Scala Users
More
GitHub
Copyright © 2020 LAMP EPFL