Mateusz Kubuszok
breaking things in Scala for 8+ years
breaking things for money for 10 years
breaking things for fun for 18(?) years
a little bit of open source - including co-authoring Chimney for 6 years now
blog at Kubuszok.com
niche Things you need to know about JVM (that matter in Scala) ebook
What is a macro method?
Expressions and types
Symbols
Examples showing similarities and differences when solving the same small problems
I will focus on examples that I saw while maintaining my libraries
I will NOT focus on macro annotations
We can intuitively think that macro method is a code generator pretending to be some object’s method.
Calling the impl method is the only thing we are allowed to do.
Expr[A]
is the AST of code that represents the value of type A
.
Why Scala 2 call it blackbox will be explained later.
Scala 3 has "global" expressions while Scala 2 use path-dependent types for them.
Impl doesn’t have to be in the same definition as unquoting - it doesn’t even have to be in the same package!
Impl has to be defined in a preceeding compilation unit to call site (macro referring to it can be in the same as call site).
Scala 2 | Scala 3 |
|
|
|
|
|
|
|
|
|
|
| no counterpart |
Scala 2 | Scala 3 |
|
|
|
|
|
|
|
|
Scala 2 | Scala 3 |
|
|
no counterpart |
|
|
|
|
|
no counterpart |
|
|
|
Scala 2 | Scala 3 |
|
|
|
|
|
|
|
|
|
|
Symbol - a reference to definition (type, class, val, var, method, parameter, binding…).
Scala 2 | Scala 3 |
|
|
|
|
| only 1 kind of |
|
|
|
|
|
|
|
|
|
|
| |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Example:
take a type of a case class
/sealed trait
try to create List
with a value of this type
for case class
create a value if all params has default value
for sealed
, create all children that can be created (case object
s lub case class
es like above)
Scala 2 | Scala 3 | ||
|
|
|
|
|
|
|
|
"macro bundle" | no counterpart |
basic concepts - typed and untyped expressions and types, AST, Symbols - are the same
Scala 2 APIs have more utilities, Scala 3 had more consistent utilities
both implementations have enough features to build upon them
both implementations have rather basic documentation
examples and slides available on my GitHub (GitHub.com/MateuszKubuszok)