What kinds of macros should Scala 3 support?


I think it will be great if scala support language injection.
Something like :

for(rs<-Sql”select a,b,c from table”){

Is it really difficult for string interpolation and java interfaces?


That paper will not be much help. The short answer is that there are two levels of abstraction/complexities in the macros with inline. The simplest abstraction is just having inline, quotes ' and splices ~ and the second add reflection capabilities.

Quotes and splices allow the implementation of generative macros. The macro will receive parameters as Expr[T] or Type[T] which can be spliced in some quoted code. These cannot inspect the trees of the parameters unless the parameters are marked as inline, in which case the value of the tree will be passed to the macro instead of an Expr[T]. This currently works with primitive value types and I am experimenting with case classes and case objects. This will not be enough for Quill.

With TASTy reflect it is possible to take some Expr[T] or Type[T] and open them to allow reflection. This is quite similar to the reflection in Scala reflect. It is implemented using abstract types, abstract extractors and abstract extension methods (the paper https://dl.acm.org/citation.cfm?id=3241658 talks about this kind of design). Currently the API does not expose all it could, we have been conservative to avoid adding unnecessary functionality in the core. It would be interesting to know what exactly is required by Quill.

Another interesting new feature that comes with TASTy reflect macros is that it is possible to inspect the trees of the code that is outside the macro itself. This might be useful to be able to get statically know information about an argument you receive even though it is defined outside.


That sounds quite neat – are there any examples of TASTy reflect macros in the tests yet?


There’s https://github.com/lampepfl/dotty/pull/5096


Cool – thanks!


https://github.com/lampepfl/dotty/pull/5096 is a good example of how to interact with TASTy trees within a macro.

Additionally, ShowSourceCode is a decompiler that takes a TASTy tree and recreates source code. It is probably the most comprehensive use of TASTy. It is not used within a macro, it uses the same interface on the tree of a complete file.


Neat – thanks!


I hope the new low-level TASTy-based reflection API will support quasiquotes, as in scala-reflect, which can construct and match code. They have proven to be really useful in that context, and were definitely one of the good parts of scala-reflect. It would be sad if the lessons learned with the scala-reflect experiment were forgotten.


It is part of the plan. Ideally, I would like to be able to support it using the quotes and splices syntax (rather than string interpolated quasiquotes) and make it a middle ground between plain generative quotes and splices and full TASTy reflection (i.e. something similar to Squid, but implemented natively).


That is fine, but I think we also need untyped quasiquotes for manipulating definitions such as classes etc.