From 4e93ea71d48d3ea360963fb3c268e58ee665aebe Mon Sep 17 00:00:00 2001 From: "Lan, Jian" Date: Mon, 30 Nov 2020 03:28:32 -0800 Subject: [PATCH 1/2] Remove an unmatched close brace --- docs/docs/reference/metaprogramming/macros.md | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/docs/docs/reference/metaprogramming/macros.md b/docs/docs/reference/metaprogramming/macros.md index 2d5713d30926..059a43f60e44 100644 --- a/docs/docs/reference/metaprogramming/macros.md +++ b/docs/docs/reference/metaprogramming/macros.md @@ -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 @@ -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 @@ -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 @@ -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. @@ -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. @@ -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) } @@ -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 @@ -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) From d7ee4ebab7967e1bd76ea39567e81b479d2198f4 Mon Sep 17 00:00:00 2001 From: "Lan, Jian" Date: Mon, 30 Nov 2020 03:30:36 -0800 Subject: [PATCH 2/2] Fix docs by replacing @infix with infix --- docs/docs/reference/contextual/extension-methods.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/docs/docs/reference/contextual/extension-methods.md b/docs/docs/reference/contextual/extension-methods.md index 77e8eef90055..7c467b859463 100644 --- a/docs/docs/reference/contextual/extension-methods.md +++ b/docs/docs/reference/contextual/extension-methods.md @@ -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) @@ -52,7 +52,7 @@ The three definitions above translate to ```scala def < (x: String)(y: String): Boolean = ... def +: (xs: Seq[Elem])(x: Elem): Seq[Elem] = ... -@infix def min(x: Number)(y: Number): Number = ... +infix def min(x: Number)(y: Number): Number = ... ``` Note the swap of the two parameters `x` and `xs` when translating @@ -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]) { @@ -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 @@ -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