Skip to content

Commit f21ab57

Browse files
committed
Revert to standard implicit syntax in docs
Better not to mix different proposals.
1 parent a1bc2f0 commit f21ab57

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

docs/docs/reference/derivation.md

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ enum Tree[T] derives Eq, Ordering, Pickling {
1212
```
1313
The `derives` clause generates implicit instances of the `Eq`, `Ordering`, and `Pickling` traits in the companion object `Tree`:
1414
```scala
15-
impl [T: Eq] of Eq[Tree[T]] = Eq.derived
16-
impl [T: Ordering] of Ordering[Tree[T]] = Ordering.derived
17-
impl [T: Pickling] of Pickling[Tree[T]] = Pickling.derived
15+
implicit def derived$Eq [T: Eq]: Eq[Tree[T]] = Eq.derived
16+
implicit def derived$Ordering [T: Ordering]: Ordering[Tree[T]] = Ordering.derived
17+
implicit def derived$Pickling [T: Pickling]: Pickling[Tree[T]] = Pickling.derived
1818
```
1919

2020
**Note**: This page uses the new syntax proposed for implicits that is explored in #5448. This is not yet an endorsement of that syntax, but rather a way to experiment with it.
@@ -47,7 +47,7 @@ A trait or class can appear in a `derives` clause if
4747

4848
These two conditions ensure that the synthesized derived instances for the trait are well-formed. The type and implementation of a `derived` method are arbitrary, but typically it has a definition like this:
4949
```scala
50-
def derived[T] with (ev: Generic[T]) = ...
50+
def derived[T](implicit ev: Generic[T]) = ...
5151
```
5252
That is, the `derived` method takes an implicit parameter of type `Generic` that determines the _shape_ of the deriving type `T` and it computes the typeclass implementation according to that shape. Implicit `Generic` instances are generated automatically for all types that have a `derives` clause. One can also derive `Generic` alone, which means a `Generic` instance is generated without any other type class instances. E.g.:
5353
```scala
@@ -100,9 +100,15 @@ is represented as `T *: Unit` since there is no direct syntax for such tuples: `
100100

101101
### The Generic TypeClass
102102

103-
For every class `C[T_1,...,T_n]` with a `derives` clause, the compiler generates in the companion object of `C` a type class instance like this:
103+
For every class `C[T_1,...,T_n]` with a `derives` clause, the compiler generates in the companion object of `C` an implicit instance of `Generic[C[T_1,...,T_n]]` that follows
104+
the outline below:
104105
```scala
105-
impl [T_1, ..., T_n] of Generic[C[T_1,...,T_n]] { type Shape = ... } = ...
106+
class derived$Generic[T_1, ..., T_n] extends Generic[C[T_1,...,T_n]] {
107+
type Shape = ...
108+
...
109+
}
110+
implicit def derived$Generic[T_1,...,T_n]]: derived$Generic[T_1,...,T_n]] =
111+
new derived$Generic[T_1,...,T_n]]
106112
```
107113
where the right hand side of `Shape` is the shape type of `C[T_1,...,T_n]`.
108114
For instance, the definition
@@ -117,13 +123,15 @@ would produce:
117123
object Result {
118124
import scala.compiletime.Shape._
119125

120-
impl [T, E] of Generic[Result[T, E]] {
126+
class derived$Generic[T, E] extends Generic[Result[T, E]] {
121127
type Shape = Cases[(
122128
Case[Ok[T], T *: Unit],
123129
Case[Err[E], E *: Unit]
124130
)]
125131
...
126132
}
133+
implicit def derived$Generic[T, E]: derived$Generic[T, E] =
134+
new derived$Generic[T, E]
127135
}
128136
```
129137
The `Generic` class is defined in package `scala.reflect`.
@@ -219,7 +227,7 @@ trait Eq[T] {
219227
We need to implement a method `Eq.derived` that produces an instance of `Eq[T]` provided
220228
there exists evidence of type `Generic[T]`. Here's a possible solution:
221229
```scala
222-
inline def derived[T, S <: Shape] with (ev: Generic[T]): Eq[T] = new Eq[T] {
230+
inline def derived[T, S <: Shape](implicit ev: Generic[T]): Eq[T] = new Eq[T] {
223231
def eql(x: T, y: T): Boolean = {
224232
val mx = ev.reflect(x) // (1)
225233
val my = ev.reflect(y) // (2)
@@ -316,7 +324,7 @@ calling the `error` method defined in `scala.compiletime`.
316324
**Example:** Here is a slightly polished and compacted version of the code that's generated by inline expansion for the derived `Eq` instance of class `Tree`.
317325

318326
```scala
319-
impl Eq_Tree_impl[T] with (elemEq: Eq[T]) of Eq[Tree[T]] {
327+
implicit def Eq_Tree_impl[T](implicit elemEq: Eq[T]): Eq[T] = new Eq[Tree[T]] {
320328
def eql(x: Tree[T], y: Tree[T]): Boolean = {
321329
val ev = implOf[Generic[Tree[T]]]
322330
val mx = ev.reflect(x)
@@ -342,7 +350,7 @@ One important difference between this approach and Scala-2 typeclass derivation
342350
Sometimes one would like to derive a typeclass instance for an ADT after the ADT is defined, without being able to change the code of the ADT itself.
343351
To do this, simply define an instance with the `derived` method of the typeclass as right hand side. E.g, to implement `Ordering` for `Option`, define:
344352
```scala
345-
impl [T: Ordering] of Ordering[Option[T]] = Ordering.derived
353+
implicit def derived$Ordering[T: Ordering]: Ordering[Option[T]] = Ordering.derived
346354
```
347355
Usually, the `Ordering.derived` clause has an implicit parameter of type
348356
`Generic[Option[T]]`. Since the `Option` trait has a `derives` clause,

0 commit comments

Comments
 (0)