This repo contains a bunch of examples of doing metaprogramming in Scala 3 using the low level reflection API.
Every folder is a separate example. Each example contains a README.md file with a description of what the example does.
To run an example:
Clone and cd into the repo using
git clone https://github.com/anatoliykmetyuk/dotty-macro-examples.git && cd dotty-macro-examples
Use ./mill <example_name>.run command to run the example you are interested in.
E.g. ./mill macroTypeClassDerivation.run runs macroTypeClassDerivation example.
String.this where the type of this may not be known on macro definition site, and call a method on this.new.Symbol and Term.Expr other than where it is defined at.dotty/library/src/scala/quoted/Quotes.scala.
As a rule, for every reflected type X, you have a group of extension methods in trait XMethods which defines all of the methods you can call on X.
For example, for Symbol, you can search for SymbolMethods in Quotes.scala to see what you can do with it.TypeTree.of[T] gives you a quotes.reflect.TypeTree. To get a quotes.reflect.TypeRepr, you can call TypeRepr.of[T].Symbols. To get a Symbol given a tpe: quoted.Type[T], use TypeTree.of[T].symbol.Symbol, you can use Ref to get a Tree referring to that symbol – either Ident or Select.
For example, if you have a sym: Symbol that refers to a DefDef method definition and you want to obtain a tree referring to that method, you can get this tree via Ref(sym).Scala
92.5%
Shell
7.5%
This repo contains a bunch of examples of doing metaprogramming in Scala 3 using the low level reflection API.
Every folder is a separate example. Each example contains a README.md file with a description of what the example does.
To run an example:
Clone and cd into the repo using
git clone https://github.com/anatoliykmetyuk/dotty-macro-examples.git && cd dotty-macro-examples
Use ./mill <example_name>.run command to run the example you are interested in.
E.g. ./mill macroTypeClassDerivation.run runs macroTypeClassDerivation example.
String.this where the type of this may not be known on macro definition site, and call a method on this.new.Symbol and Term.Expr other than where it is defined at.dotty/library/src/scala/quoted/Quotes.scala.
As a rule, for every reflected type X, you have a group of extension methods in trait XMethods which defines all of the methods you can call on X.
For example, for Symbol, you can search for SymbolMethods in Quotes.scala to see what you can do with it.TypeTree.of[T] gives you a quotes.reflect.TypeTree. To get a quotes.reflect.TypeRepr, you can call TypeRepr.of[T].Symbols. To get a Symbol given a tpe: quoted.Type[T], use TypeTree.of[T].symbol.Symbol, you can use Ref to get a Tree referring to that symbol – either Ident or Select.
For example, if you have a sym: Symbol that refers to a DefDef method definition and you want to obtain a tree referring to that method, you can get this tree via Ref(sym).Scala
92.5%
Shell
7.5%