Skip to content

Commit ccaee95

Browse files
committed
Merge pull request #378 from xeno-by/master
fights bit rot in macro docs
2 parents f9a03da + d63e737 commit ccaee95

File tree

12 files changed

+86
-39
lines changed

12 files changed

+86
-39
lines changed

overviews/macros/annotations.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ languages: [ja]
1313

1414
**Eugene Burmako**
1515

16-
Macro annotations are only available in Scala 2.10.x and 2.11.x with the macro paradise plugin.
17-
Their inclusion in official Scala might happen in Scala 2.12, but there is no certainty about it yet.
16+
Macro annotations are only available with the macro paradise plugin (in Scala 2.10.x, 2.11.x and 2.12.x alike).
17+
Their inclusion in official Scala might happen in Scala 2.13, but there is no certainty about it yet.
1818
Follow the instructions at the ["Macro Paradise"](/overviews/macros/paradise.html) page to download and use our compiler plugin.
1919

2020
Note that macro paradise is needed both to compile and to expand macro annotations,
@@ -94,3 +94,8 @@ as typed as possible to remain useful. On the one hand, macro annottees are unty
9494
of class members). But on the other hand, the thing about all flavors of Scala macros is integration with the typechecker, and
9595
macro annotations are not an exceptions. During expansion we can have all the type information that's possible to have
9696
(e.g. we can reflect against the surrounding program or perform type checks / implicit lookups in the enclosing context).
97+
98+
## Blackbox vs whitebox
99+
100+
Macro annotations must be [whitebox](/overviews/macros/blackbox-whitebox.html).
101+
If you declare a macro annotation as [blackbox](/overviews/macros/blackbox-whitebox.html), it will not work.

overviews/macros/blackbox-whitebox.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,22 @@ disqus: true
66

77
partof: macros
88
num: 2
9+
outof: 13
910
---
1011
<span class="label warning" style="float: right;">EXPERIMENTAL</span>
1112

1213
**Eugene Burmako**
1314

14-
Separation of macros into blackbox ones and whitebox ones is implemented in the recent milestone builds of Scala 2.11, starting from 2.11.0-M7 (however, in 2.11.0-M8, the syntax underwent some changes, so this documentation isn't applicable to earlier milestone builds of 2.11). The blackbox/whitebox separation is not implemented in Scala 2.10.x or in macro paradise. Follow the instructions at [http://www.scala-lang.org/download](http://www.scala-lang.org/download) to download and use the latest milestone of 2.11.
15+
Separation of macros into blackbox ones and whitebox ones is a feature of Scala 2.11.x and Scala 2.12.x. The blackbox/whitebox separation is not supported in Scala 2.10.x. It is also not supported in macro paradise for Scala 2.10.x.
1516

1617
## Why macros work?
1718

1819
With macros becoming a part of the official Scala 2.10 release, programmers in research and industry have found creative ways of using macros to address all sorts of problems, far extending our original expectations.
1920

2021
In fact, macros became an important part of our ecosystem so quickly that just a couple months after the release of Scala 2.10, when macros were introduced in experimental capacity, we had a Scala language team meeting and decided to standardize macros and make them a full-fledged feature of Scala by 2.12.
2122

23+
<span class="label success">UPDATE</span> It turned out that it was not that simple to stabilize macros by Scala 2.12. Our research into that has resulted in establishing a new metaprogramming foundation for Scala, called [scala.meta](http://scalameta.org), whose first beta is expected to be released simultaneously with Scala 2.12 and might later be included in future versions of Scala. In the meanwhile, Scala 2.12 is not going to see any changes to reflection and macros - everything is going to stay experimental as it was in Scala 2.10 and Scala 2.11, and no features are going to be removed. However, even though circumstances under which this document has been written have changed, the information still remains relevant, so please continue reading.
24+
2225
Macro flavors are plentiful, so we decided to carefully examine them to figure out which ones should be put in the standard. This entails answering a few important questions. Why are macros working so well? Why do people use them?
2326

2427
Our hypothesis is that this happens because the hard to comprehend notion of metaprogramming expressed in def macros piggybacks on the familiar concept of a typed method call. Thanks to that, the code that users write can absorb more meaning without becoming bloated or losing
@@ -32,7 +35,7 @@ This curious feature provides additional flexibility, enabling [fake type provid
3235

3336
To concretize the crucial distinction between macros that behave just like normal methods and macros that refine their return types, we introduce the notions of blackbox macros and whitebox macros. Macros that faithfully follow their type signatures are called **blackbox macros** as their implementations are irrelevant to understanding their behaviour (could be treated as black boxes). Macros that can't have precise signatures in Scala's type system are called **whitebox macros** (whitebox def macros do have signatures, but these signatures are only approximations).
3437

35-
We recognize the importance of both blackbox and whitebox macros, however we feel more confidence in blackbox macros, because they are easier to explain, specify and support. Therefore our plans to standardize macros in Scala 2.12 only include blackbox macros. In future releases of Scala we might make whitebox macros non-experimental as well, but it is too early to tell.
38+
We recognize the importance of both blackbox and whitebox macros, however we feel more confidence in blackbox macros, because they are easier to explain, specify and support. Therefore our plans to standardize macros only include blackbox macros. Later on, we might also include whitebox macros into our plans, but it's too early to tell.
3639

3740
## Codifying the distinction
3841

@@ -47,4 +50,4 @@ Blackbox def macros are treated differently from def macros of Scala 2.10. The f
4750
1. When an application of a blackbox macro is used as an implicit candidate, no expansion is performed until the macro is selected as the result of the implicit search. This makes it impossible to [dynamically calculate availability of implicit macros](/sips/pending/source-locations.html).
4851
1. When an application of a blackbox macro is used as an extractor in a pattern match, it triggers an unconditional compiler error, preventing [customizations of pattern matching](https://github.com/paulp/scala/commit/84a335916556cb0fe939d1c51f27d80d9cf980dc) implemented with macros.
4952

50-
Whitebox def macros work exactly like def macros used to work in Scala 2.10. No restrictions of any kind get applied, so everything that could be done with macros in 2.10 should be possible in 2.11.
53+
Whitebox def macros work exactly like def macros used to work in Scala 2.10. No restrictions of any kind get applied, so everything that could be done with macros in 2.10 should be possible in 2.11 and 2.12.

overviews/macros/bundles.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,18 @@ disqus: true
66

77
partof: macros
88
num: 5
9+
outof: 13
910
languages: [ja]
1011
---
1112
<span class="label warning" style="float: right;">EXPERIMENTAL</span>
1213

1314
**Eugene Burmako**
1415

15-
Macro bundles are shipped with the recent milestone builds of Scala 2.11, starting from 2.11.0-M4 (however, in 2.11.0-M8, the syntax underwent some changes, so this documentation isn't applicable to earlier milestone builds of 2.11). Macro bundles are not available in Scala 2.10.x or in macro paradise. Follow the instructions at [http://www.scala-lang.org/download/](http://www.scala-lang.org/download/) to download and use the latest milestone of 2.11.
16+
Macro bundles are a feature of Scala 2.11.x and Scala 2.12.x. Macro bundles are not supported in Scala 2.10.x. They are also not supported in macro paradise for Scala 2.10.x.
1617

1718
## Macro bundles
1819

19-
Currently, in Scala 2.10.x, macro implementations are represented with functions. Once the compiler sees an application of a macro definition,
20+
In Scala 2.10.x, macro implementations are represented with functions. Once the compiler sees an application of a macro definition,
2021
it calls the macro implementation - as simple as that. However practice shows that just functions are often not enough due to the
2122
following reasons:
2223

@@ -26,7 +27,7 @@ traits outside macro implementations, turning implementations into trivial wrapp
2627
2. Moreover, since macro parameters are path-dependent on the macro context, [special incantations](/overviews/macros/overview.html#writing_bigger_macros) are required to wire implementations and helpers together.
2728

2829
Macro bundles provide a solution to these problems by allowing macro implementations to be declared in classes that take
29-
`c: BlackboxContext` or `c: WhiteboxContext` as their constructor parameters, relieving macro implementations from having
30+
`c: scala.reflect.macros.blackbox.Context` or `c: scala.reflect.macros.whitebox.Context` as their constructor parameters, relieving macro implementations from having
3031
to declare the context in their signatures, which simplifies modularization. Referencing macro implementations defined in bundles
3132
works in the same way as with impls defined in objects. You specify a bundle name and then select a method from it,
3233
providing type arguments if necessary.
@@ -42,3 +43,7 @@ providing type arguments if necessary.
4243
def mono = macro Impl.mono
4344
def poly[T] = macro Impl.poly[T]
4445
}
46+
47+
## Blackbox vs whitebox
48+
49+
Macro bundles can be used to implement both [blackbox](/overviews/macros/blackbox-whitebox.html) and [whitebox](/overviews/macros/blackbox-whitebox.html) macros. Give the macro bundle constructor parameter the type of `scala.reflect.macros.blackbox.Context` to define a blackbox macro and the type of `scala.reflect.macros.whitebox.Context` to define a whitebox macro.

overviews/macros/changelog211.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ disqus: true
66

77
partof: macros
88
num: 13
9+
outof: 13
910
---
1011

1112
<span class="label warning" style="float: right;">EXPERIMENTAL</span>

overviews/macros/extractors.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@ disqus: true
66

77
partof: macros
88
num: 7
9+
outof: 13
910
---
1011
<span class="label warning" style="float: right;">EXPERIMENTAL</span>
1112

1213
**Eugene Burmako**
1314

14-
Extractor macros are shipped with the recent milestone builds of Scala 2.11, starting from 2.11.0-M5, enabled by name-based extractors introduced by Paul Phillips in Scala 2.11.0-M5. Extractor macros are not available in Scala 2.10.x or in macro paradise. Follow the instructions at [http://www.scala-lang.org/download/](http://www.scala-lang.org/download/) to download and use the latest milestone of 2.11.
15+
Extractor macros are a feature of Scala 2.11.x and Scala 2.12.x, enabled by name-based extractors introduced by Paul Phillips in Scala 2.11.0-M5. Extractor macros are not supported in Scala 2.10.x. They are also not supported in macro paradise for Scala 2.10.x.
1516

16-
### The pattern
17+
## The pattern
1718

1819
In a nutshell, given an unapply method (for simplicity, in this
1920
example the scrutinee is of a concrete type, but it's also possible
@@ -61,7 +62,7 @@ because one can't declare value classes local. Nevertheless,
6162
I'm leaving a canary in place ([neg/t5903e](https://github.com/scala/scala/blob/00624a39ed84c3fd245dd9df7454d4cec4399e13/test/files/neg/t5903e/Macros_1.scala#L1)) that will let us know
6263
once this restriction is lifted.
6364

64-
### Use cases
65+
## Use cases
6566

6667
In particular, the pattern can be used to implement shapeshifting
6768
pattern matchers for string interpolators without resorting to dirty
@@ -87,4 +88,7 @@ Follow our test cases at [run/t5903a](https://github.com/scala/scala/tree/00624a
8788
[run/t5903d](https://github.com/scala/scala/tree/00624a39ed84c3fd245dd9df7454d4cec4399e13/test/files/run/t5903d) to see implementations
8889
of this and other use cases for extractor macros.
8990

90-
Please note that extractor macros must be [whitebox](/overviews/macros/blackbox-whitebox.html), otherwise they will not work.
91+
## Blackbox vs whitebox
92+
93+
Extractor macros must be [whitebox](/overviews/macros/blackbox-whitebox.html).
94+
If you declare an extractor macro as [blackbox](/overviews/macros/blackbox-whitebox.html), it will not work.

overviews/macros/implicits.md

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ disqus: true
66

77
partof: macros
88
num: 6
9+
outof: 13
910
languages: [ja]
1011
---
1112
<span class="label warning" style="float: right;">EXPERIMENTAL</span>
@@ -17,12 +18,14 @@ but require a critical bugfix in 2.10.2 to become fully operational. Implicit ma
1718
neither in 2.10.x, nor in 2.11.
1819

1920
An extension to implicit macros,
20-
called fundep materialization, is unavailable in 2.10.x, but has been implemented both in
21-
[macro paradise](/overviews/macros/paradise.html) and Scala 2.11.0-M5.
22-
Note that in 2.10.x, expansion of fundep materializer macros requires macro paradise,
21+
called fundep materialization, is unavailable in 2.10.0 through 2.10.4, but has been implemented in
22+
[macro paradise](/overviews/macros/paradise.html), Scala 2.10.5 and Scala 2.11.x.
23+
Note that in 2.10.0 through 2.10.4, expansion of fundep materializer macros requires macro paradise,
2324
which means that your users will have to add macro paradise to their builds in order to use your fundep materializers.
2425
However, after fundep materializers expand, the resulting code will no longer have any references to macro paradise
25-
and won't require its presence at compile-time or at runtime.
26+
and won't require its presence at compile-time or at runtime. Also note that in 2.10.5, expansion of
27+
fundep materializer macros can happen without macro paradise, but then your users will have to enable
28+
the <code>-Yfundep-materialization</code> compiler flag.
2629

2730
## Implicit macros
2831

@@ -147,4 +150,12 @@ Note how simple everything is. The `materializeIso` implicit macro just takes it
147150
We don't need to make sense of the second type argument (which isn't inferred yet), we don't need to interact with type inference -
148151
everything happens automatically.
149152

150-
Please note that there is [a funny caveat](https://github.com/scala/scala/blob/7b890f71ecd0d28c1a1b81b7abfe8e0c11bfeb71/test/files/run/t5923a/Macros_1.scala) with Nothings that we plan to address later. Also note that fundep materializers must be [whitebox](/overviews/macros/blackbox-whitebox.html), otherwise they will not work.
153+
Please note that there is [a funny caveat](https://github.com/scala/scala/blob/7b890f71ecd0d28c1a1b81b7abfe8e0c11bfeb71/test/files/run/t5923a/Macros_1.scala) with Nothings that we plan to address later.
154+
155+
## Blackbox vs whitebox
156+
157+
Vanilla materializers (covered in the first part of this document) can be both [blackbox](/overviews/macros/blackbox-whitebox.html) and [whitebox](/overviews/macros/blackbox-whitebox.html).
158+
159+
There is a noticeable distinction between blackbox and whitebox materializers. An error in an expansion of a blackbox implicit macro (e.g. an explicit <code>c.abort</code> call or an expansion typecheck error) will produce a compilation error. An error in an expansion of a whitebox implicit macro will just remove the macro from the list of implicit candidates in the current implicit search, without ever reporting an actual error to the user. This creates a trade-off: blackbox implicit macros feature better error reporting, while whitebox implicit macros are more flexible, being able to dynamically turn themselves off when necessary.
160+
161+
Fundep materializers must be whitebox. If you declare a fundep materializer as blackbox, it will not work.

overviews/macros/overview.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ disqus: true
66

77
partof: macros
88
num: 3
9+
outof: 13
910
languages: [ja]
1011
---
1112
<span class="label warning" style="float: right;">EXPERIMENTAL</span>
@@ -15,6 +16,14 @@ languages: [ja]
1516
Def macros are shipped as an experimental feature of Scala since version 2.10.0.
1617
A subset of def macros, pending a thorough specification, is tentatively scheduled to become stable in one of the future versions of Scala.
1718

19+
<span class="label success">UPDATE</span> This guide has been written for Scala 2.10.0, and now we're well into the Scala 2.11.x release cycle,
20+
so naturally the contents of the document are outdated. Nevertheless, this guide is not obsolete -
21+
everything written here will still work in both Scala 2.10.x and Scala 2.11.x, so it will be helpful to read it through.
22+
After reading the guide, take a look at the docs on [quasiquotes](/overviews/macros/quasiquotes.html)
23+
and [macro bundles](/overviews/macros/bundles.html) to familiarize yourself with latest developments
24+
that dramatically simplify writing macros. Then it might be a good idea to follow
25+
[our macro workshop](https://github.com/scalamacros/macrology201) for more in-depth examples.
26+
1827
## Intuition
1928

2029
Here is a prototypical macro definition:

overviews/macros/paradise.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ disqus: true
66

77
partof: macros
88
num: 11
9+
outof: 13
910
languages: [ja]
1011
---
1112
<span class="label success" style="float: right;">NEW</span>
@@ -56,4 +57,4 @@ To use macro paradise in Maven follow the instructions provided at Stack Overflo
5657

5758
Sources of macro paradise are available at [https://github.com/scalamacros/paradise](https://github.com/scalamacros/paradise).
5859
There are branches that support the latest 2.10.x release, the latest 2.11.x release,
59-
snapshots of 2.10.x and 2.11.x, as well as Scala virtualized.
60+
snapshots of 2.10.x, 2.11.x and 2.12.x, as well as Scala virtualized.

overviews/macros/quasiquotes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ disqus: true
66

77
partof: macros
88
num: 4
9+
outof: 13
910
languages: [ja]
1011
---
1112

0 commit comments

Comments
 (0)