Skip to content

Commit 1d710ca

Browse files
Add more metaprogramming changes
1 parent cbc8757 commit 1d710ca

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

docs/blog/_posts/2020-02-03-22nd-dotty-milestone-release.md

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,33 @@ Translates to:
141141
a + a + b + b + h() + h()
142142
```
143143

144-
This change was introduced by [PR #8060](https://github.com/lampepfl/dotty/pull/8060/). For more information about the inline capability of Dotty, see [documentation](https://dotty.epfl.ch/docs/reference/metaprogramming/inline.html).
144+
Notice how the value of the by-name parameter `b` is not inlined but is bound to `def b`. This is an important change that affects all the macros that accepted by-name parameters and analyzed the AST of the underlying code. With this release, such macros will stop working correctly because the AST of the code in question will be the identifier of the by-name parameter, `b` in this case, and not the AST of the code passed under that parameter's name. The workaround is to change all the by-name parameters in your macros to inline parameters.
145+
146+
So, if previously you had a macro `inline def operationOnCode(code: => Unit) = ${ mcrImpl('code) }` which did something on the AST of the passed `code`, with this release you need to change it to `inline def operationOnCode(inline code: Unit) = ${ mcrImpl('code) }`.
147+
148+
This change was introduced by [PR #8060](https://github.com/lampepfl/dotty/pull/8060/).
149+
150+
Another change in the semantics of the inline parameters involves the fact that the can no longer be passed as constants to macro implementations. Previously, the following was possible:
151+
152+
```scala
153+
// OLD SEMANTICS
154+
inline def power(x: Double, inline n: Int) = ${ powerCode('x, n) }
155+
private def powerCode(x: Expr[Double], n: Int)(given
156+
QuoteContext): Expr[Double] = ???
157+
```
158+
159+
It was possible to pass `n` directly to the spliced `powerCode` and it would have been treated as a constant in that macro implementation.
160+
161+
Now, the inline parameters must be quoted when passed to a macro:
162+
163+
```scala
164+
inline def power(x: Double, inline n: Int) = ${ powerCode('x, 'n) }
165+
private def powerCode(x: Expr[Double], n: Expr[Int])(given QuoteContext): Expr[Double] = ???
166+
```
167+
168+
You can obtain the constant value of `n` from within the macro implementation by calling `n.getValue` on it which returns an `Option`. This change was introduced by [PR #8061](https://github.com/lampepfl/dotty/pull/8061).
169+
170+
For more information about the inline capability of Dotty, see [documentation](https://dotty.epfl.ch/docs/reference/metaprogramming/inline.html).
145171

146172
# Primitive compiletime operations on singleton types
147173
Contributed by [Maxime Kjaer](https://github.com/MaximeKjaer), this release brings along type-level arithmetic:
@@ -184,6 +210,9 @@ One area where these suggestions will make life easier is purely functional prog
184210

185211
For the discussion, see [PR #7862](https://github.com/lampepfl/dotty/pull/7862).
186212

213+
# TASTy Inspector library
214+
TASTy Consumer was renamed to TASTy Inspector as of this release. It was also published in a library of its own. For more information, see the [documentation](https://dotty.epfl.ch/docs/reference/metaprogramming/tasty-inspect.html) on this library.
215+
187216
# Let us know what you think!
188217

189218
If you have questions or any sort of feedback, feel free to send us a message on our

0 commit comments

Comments
 (0)