Skip to content

Fix docs #10555

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 2 commits into from
Nov 30, 2020
Merged

Fix docs #10555

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
8 changes: 5 additions & 3 deletions docs/docs/reference/contextual/extension-methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ extension (x: String)
extension (x: Elem)
def +: (xs: Seq[Elem]): Seq[Elem] = ...
extension (x: Number)
@infix def min (y: Number): Number = ...
infix def min (y: Number): Number = ...

"ab" < "c"
1 +: List(2, 3)
Expand All @@ -52,7 +52,7 @@ The three definitions above translate to
```scala
<extension> def < (x: String)(y: String): Boolean = ...
<extension> def +: (xs: Seq[Elem])(x: Elem): Seq[Elem] = ...
@infix <extension> def min(x: Number)(y: Number): Number = ...
infix <extension> def min(x: Number)(y: Number): Number = ...
```

Note the swap of the two parameters `x` and `xs` when translating
Expand Down Expand Up @@ -113,6 +113,7 @@ extension (ss: Seq[String]):
```

The same can be written with braces as follows (note that indented regions can still be used inside braces):

```scala
extension (ss: Seq[String]) {

Expand Down Expand Up @@ -239,11 +240,13 @@ The following two rewritings are tried in order:
from `T` to a type containing `m`. If there is more than one way of rewriting, an ambiguity error results.

An extension method can also be referenced using a simple identifier without a preceding expression. If an identifier `g` appears in the body of an extension method `f` and refers to an extension method `g` that is defined in the same collective extension

```scala
extension (x: T)
def f ... = ... g ...
def g ...
```

the identifier is rewritten to `x.g`. This is also the case if `f` and `g` are the same method. Example:

```scala
Expand All @@ -261,7 +264,6 @@ def position(s: String)(ch: Char, n: Int): Int =
else n
```


### Syntax

Here are the syntax changes for extension methods and collective extensions relative
Expand Down
18 changes: 11 additions & 7 deletions docs/docs/reference/metaprogramming/macros.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,14 @@ is treated as a quote `'{x}`. See the Syntax section below for details.

Quotes and splices are duals of each other. For arbitrary
expressions `e` and types `T` we have:
```

```scala
${'{e}} = e
'{${e}} = e
${'[T]} = T
'[${T}] = T
```

### Types for Quotations

The type signatures of quotes and splices can be described using
Expand Down Expand Up @@ -202,11 +204,14 @@ For instance, the user-level definition of `to`:
def to[T, R](f: Expr[T] => Expr[R])(using t: Type[T], r: Type[R], Quotes): Expr[T => R] =
'{ (x: T) => ${ f('x) } }
```

would be rewritten to

```scala
def to[T, R](f: Expr[T] => Expr[R])(using t: Type[T], r: Type[R], Quotes): Expr[T => R] =
'{ (x: t.Underlying }) => ${ f('x) } }
'{ (x: t.Underlying) => ${ f('x) } }
```

The `summon` query succeeds because there is a given instance of
type `Type[T]` available (namely the given parameter corresponding
to the context bound `: Type`), and the reference to that value is
Expand Down Expand Up @@ -328,7 +333,6 @@ def showExpr[T](expr: Expr[T])(using Quotes): Expr[String] = {
That is, the `showExpr` method converts its `Expr` argument to a string (`code`), and lifts
the result back to an `Expr[String]` using `Expr.apply`.


### Lifting Types

The previous section has shown that the metaprogramming framework has
Expand Down Expand Up @@ -612,6 +616,7 @@ val b: String = defaultOf("string")
```

### Defining a macro and using it in a single project

It is possible to define macros and use them in the same project as long as the implementation
of the macros does not have run-time dependencies on code in the file where it is used.
It might still have compile-time dependencies on types and quoted code that refers to the use-site file.
Expand All @@ -622,7 +627,6 @@ If there are any suspended files when the compilation ends, the compiler will au
compilation of the suspended files using the output of the previous (partial) compilation as macro classpath.
In case all files are suspended due to cyclic dependencies the compilation will fail with an error.


### Pattern matching on quoted expressions

It is possible to deconstruct or extract values out of `Expr` using pattern matching.
Expand Down Expand Up @@ -712,7 +716,6 @@ def f(exp: Expr[Any])(using Quotes) =

This might be used to then perform an implicit search as in:


```scala
extension (inline sc: StringContext) inline def showMe(inline args: Any*): String = ${ showMeExpr('sc, 'args) }

Expand Down Expand Up @@ -744,7 +747,8 @@ trait Show[-T] {

Quote pattern matching also provides higher-order patterns to match open terms. If a quoted term contains a definition,
then the rest of the quote can refer to this definition.
```

```scala
'{
val x: Int = 4
x * x
Expand Down Expand Up @@ -785,6 +789,6 @@ eval { // expands to the code: (16: Int)
We can also close over several bindings using `$b(a1, a2, ..., an)`.
To match an actual application we can use braces on the function part `${b}(a1, a2, ..., an)`.


### More details

[More details](./macros-spec.md)