Skip to content

updates bundles and boxes to fit recent changes to 2.11.0-SNAPSHOT #282

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 14, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions overviews/macros/blackbox-whitebox.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ outof: 11

**Eugene Burmako**

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. It 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.
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 *box 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.

## Why macros work?

Expand All @@ -39,9 +39,7 @@ We recognize the importance of both blackbox and whitebox macros, however we fee

In the 2.11 release, we take first step of standardization by expressing the distinction between blackbox and whitebox macros in signatures of def macros, so that `scalac` can treat such macros differently. This is just a preparatory step, so both blackbox and whitebox macros remain experimental in Scala 2.11.

We express the distinction by replacing `scala.reflect.macros.Context` with `scala.reflect.macros.BlackboxContext` and `scala.reflect.macros.WhiteboxContext`. If a macro impl is defined with `BlackboxContext` as its first argument, then macro defs that are using it are considered blackbox, and analogously for `WhiteboxContext`. Of course, the vanilla `Context` is still there for compatibility reasons, but it issues a deprecation warning encouraging to choose between blackbox and whitebox macros.

[Macro bundles](/overviews/macros/bundles.html) also support the distinction by providing two different traits to inherit from: `scala.reflect.macros.BlackboxMacro` and `scala.reflect.macros.WhiteboxMacro`. Unsurprisingly, the former provides a `BlackboxContext` and turns its users into blackbox macros, and the latter provides a `WhiteboxContext` and makes def macros referring to it to be whitebox macros.
We express the distinction by replacing `scala.reflect.macros.Context` with `scala.reflect.macros.blackbox.Context` and `scala.reflect.macros.whitebox.Context`. If a macro impl is defined with `blackbox.Context` as its first argument, then macro defs that are using it are considered blackbox, and analogously for `whitebox.Context`. Of course, the vanilla `Context` is still there for compatibility reasons, but it issues a deprecation warning encouraging to choose between blackbox and whitebox macros.

Blackbox def macros are treated differently from def macros of Scala 2.10. The following restrictions are applied to them by the Scala typechecker:

Expand Down
27 changes: 8 additions & 19 deletions overviews/macros/bundles.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ languages: [ja]

**Eugene Burmako**

Macro bundles are shipped with the recent milestone builds of Scala 2.11, starting from 2.11.0-M4. They 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.
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.

## Macro bundles

Expand All @@ -26,26 +26,15 @@ traits outside macro implementations, turning implementations into trivial wrapp

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.

Macro bundles provide a solution to these problems by allowing macro implementations to be declared in traits, which extend
`scala.reflect.macros.BlackboxMacro` or `scala.reflect.macros.WhiteboxMacro`. These base traits predefine `val c: BlackboxContext`
and `val c: WhiteboxContext` correspondingly, relieving macro implementations from having to declare the context in their signatures,
which simplifies modularization.
Macro bundles provide a solution to these problems by allowing macro implementations to be declared in classes that take
`c: BlackboxContext` or `c: WhiteboxContext` as their constructor parameters, relieving macro implementations from having
to declare the context in their signatures, which simplifies modularization. Referencing macro implementations defined in bundles
works in the same way as with impls defined in objects. You specify a bundle name and then select a method from it,
providing type arguments if necessary.

trait BlackboxMacro {
val c: BlackboxContext
}

trait WhiteboxMacro {
val c: WhiteboxContext
}

Referencing macro implementations defined in bundles works in the same way as with impls defined in objects. You specify a bundle name
and then select a method from it, providing type arguments if necessary.

import scala.reflect.macros.Context
import scala.reflect.macros.BlackboxMacro
import scala.reflect.macros.blackbox.Context

trait Impl extends BlackboxMacro {
class Impl(val c: Context) {
def mono = c.literalUnit
def poly[T: c.WeakTypeTag] = c.literal(c.weakTypeOf[T].toString)
}
Expand Down