Skip to content
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

Some fix to macros docs (#7608) #7609

Merged
merged 2 commits into from
Nov 24, 2019
Merged
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
17 changes: 9 additions & 8 deletions docs/docs/reference/metaprogramming/macros.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ import scala.quoted._
inline def assert(expr: => Boolean): Unit =
${ assertImpl('expr) }

def assertImpl(expr: Expr[Boolean]) = '{
def assertImpl(expr: Expr[Boolean])(given QuoteContext) = '{
if (!$expr)
throw new AssertionError(s"failed assertion: ${${ showExpr(expr) }}")
}

def showExpr(expr: Expr[Boolean]): Expr[String] =
def showExpr(expr: Expr[Boolean])(given QuoteContext): Expr[String] =
'{ "<some source code>" } // Better implementation later in this document
```

Expand Down Expand Up @@ -238,22 +238,23 @@ Running `compile(letExp, Map())` would yield the following Scala code:
'{ val y = 3; (2 + y) + 4 }
```
The body of the first clause, `case Num(n) => Expr(n)`, looks suspicious. `n`
is declared as an `Int`, yet it is converted to an `Expr[Int]` with `toExpr`.
is declared as an `Int`, yet it is converted to an `Expr[Int]` with `Expr()`.
Shouldn’t `n` be quoted? In fact this would not
work since replacing `n` by `'n` in the clause would not be phase
correct.

The `toExpr` extension method is defined in package `quoted`:
The `Expr.apply` method is defined in package `quoted`:
```scala
package quoted

given {
def (x: T) toExpr[T: Liftable](given QuoteContext): Expr[T] = summon[Liftable[T]].toExpr(x)
object Expr {
...
def apply[T: Liftable](x: T)(given QuoteContext): Expr[T] = summon[Liftable[T]].toExpr(x)
...
}
```
The extension says that values of types implementing the `Liftable` type class can be
converted ("lifted") to `Expr` values using `toExpr`, provided a given import of `scala.quoted._` is in scope.
This method says that values of types implementing the `Liftable` type class can be
converted ("lifted") to `Expr` values using `Expr.apply`.

Dotty comes with given instances of `Liftable` for
several types including `Boolean`, `String`, and all primitive number
Expand Down