Skip to content

Commit b675a41

Browse files
committed
Use new extension method syntax in instnace-defs doc
1 parent cd33fa4 commit b675a41

File tree

1 file changed

+21
-21
lines changed

1 file changed

+21
-21
lines changed

docs/docs/reference/instances/instance-defs.md

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,18 @@ Instance definitions provide a concise and uniform syntax for defining implicit
77

88
```scala
99
trait Ord[T] {
10-
def compareTo(this x: T)(y: T): Int
11-
def < (this x: T)(y: T) = x.compareTo(y) < 0
12-
def > (this x: T)(y: T) = x.compareTo(y) > 0
10+
def (x: T) compareTo (y: T): Int
11+
def (x: T) < (y: T) = x.compareTo(y) < 0
12+
def (x: T) > (y: T) = x.compareTo(y) > 0
1313
}
1414

1515
instance IntOrd of Ord[Int] {
16-
def compareTo(this x: Int)(y: Int) =
16+
def (x: Int) compareTo (y: Int) =
1717
if (x < y) -1 else if (x > y) +1 else 0
1818
}
1919

2020
instance ListOrd[T: Ord] of Ord[List[T]] {
21-
def compareTo(this xs: List[T])(ys: List[T]): Int = (xs, ys) match {
21+
def (xs: List[T]) compareTo (ys: List[T]): Int = (xs, ys) match {
2222
case (Nil, Nil) => 0
2323
case (Nil, _) => -1
2424
case (_, Nil) => +1
@@ -32,12 +32,12 @@ instance ListOrd[T: Ord] of Ord[List[T]] {
3232
Instance can be seen as shorthands for what is currently expressed as implicit definitions. The instance definitions above could also have been formulated as implicits as follows:
3333
```scala
3434
implicit object IntOrd extends Ord[Int] {
35-
def compareTo(this x: Int)(y: Int) =
35+
def (x: Int) compareTo (y: Int) =
3636
if (x < y) -1 else if (x > y) +1 else 0
3737
}
3838

3939
class ListOrd[T: Ord] extends Ord[List[T]] {
40-
def compareTo(this xs: List[T])(ys: List[T]): Int = (xs, ys) match {
40+
def (xs: List[T]) compareTo (ys: List[T]): Int = (xs, ys) match {
4141
case (Nil, Nil) => 0
4242
case (Nil, _) => -1
4343
case (_, Nil) => +1
@@ -60,14 +60,14 @@ Instances can also be defined without an `of` clause. A typical application is t
6060

6161
```scala
6262
instance StringOps {
63-
def longestStrings(this xs: Seq[String]): Seq[String] = {
63+
def (xs: Seq[String]) longestStrings: Seq[String] = {
6464
val maxLength = xs.map(_.length).max
6565
xs.filter(_.length == maxLength)
6666
}
6767
}
6868

6969
instance ListOps {
70-
def second[T](this xs: List[T]) = xs.tail.head
70+
def (xs: List[T]) second[T] = xs.tail.head
7171
}
7272
```
7373
Instances like these translate to `implicit` objects without an extends clause.
@@ -80,7 +80,7 @@ instance of Ord[Int] { ... }
8080
instance [T: Ord] of Ord[List[T]] { ... }
8181

8282
instance {
83-
def second[T](this xs: List[T]) = xs.tail.head
83+
def (xs: List[T]) second[T] = xs.tail.head
8484
}
8585
```
8686
If the name of an instance is missing, the compiler will synthesize a name from
@@ -92,11 +92,11 @@ extension method. Details remain to be specified.
9292
An instance definition can depend on another instance being defined. For instance:
9393
```scala
9494
trait Convertible[From, To] {
95-
def convert (this x: From): To
95+
def convert(x: From): To
9696
}
9797

9898
instance [From, To] with (c: Convertible[From, To]) of Convertible[List[From], List[To]] {
99-
def convert (this x: List[From]): List[To] = x.map(c.convert)
99+
def convert(x: List[From]): List[To] = x.map(c.convert)
100100
}
101101
```
102102

@@ -105,7 +105,7 @@ The `with` clause instance defines required instances. The instance of `Converti
105105
```scala
106106
class Convertible_List_List_instance[From, To](implicit c: Convertible[From, To])
107107
extends Convertible[List[From], List[To]] {
108-
def convert (this x: List[From]): List[To] = x.map(c.convert)
108+
def convert (x: List[From]): List[To] = x.map(c.convert)
109109
}
110110
implicit def Convertible_List_List_instance[From, To](implicit c: Convertible[From, To])
111111
: Convertible[List[From], List[To]] =
@@ -132,14 +132,14 @@ Semigroups and monoids:
132132

133133
```scala
134134
trait SemiGroup[T] {
135-
def combine(this x: T)(y: T): T
135+
def (x: T) combine (y: T): T
136136
}
137137
trait Monoid[T] extends SemiGroup[T] {
138138
def unit: T
139139
}
140140

141141
instance of Monoid[String] {
142-
def combine(this x: String)(y: String): String = x.concat(y)
142+
def (x: String) combine (y: String): String = x.concat(y)
143143
def unit: String = ""
144144
}
145145

@@ -149,25 +149,25 @@ def sum[T: Monoid](xs: List[T]): T =
149149
Functors and monads:
150150
```scala
151151
trait Functor[F[_]] {
152-
def map[A, B](this x: F[A])(f: A => B): F[B]
152+
def (x: F[A]) map[A, B] (f: A => B): F[B]
153153
}
154154

155155
trait Monad[F[_]] extends Functor[F] {
156-
def flatMap[A, B](this x: F[A])(f: A => F[B]): F[B]
157-
def map[A, B](this x: F[A])(f: A => B) = x.flatMap(f `andThen` pure)
156+
def (x: F[A]) flatMap[A, B] (f: A => F[B]): F[B]
157+
def (x: F[A]) map[A, B] (f: A => B) = x.flatMap(f `andThen` pure)
158158

159159
def pure[A](x: A): F[A]
160160
}
161161

162162
instance ListMonad of Monad[List] {
163-
def flatMap[A, B](this xs: List[A])(f: A => List[B]): List[B] =
163+
def (xs: List[A]) flatMap[A, B] (f: A => List[B]): List[B] =
164164
xs.flatMap(f)
165165
def pure[A](x: A): List[A] =
166166
List(x)
167167
}
168168

169169
instance ReaderMonad[Ctx] of Monad[[X] => Ctx => X] {
170-
def flatMap[A, B](this r: Ctx => A)(f: A => Ctx => B): Ctx => B =
170+
def (r: Ctx => A) flatMap[A, B] (f: A => Ctx => B): Ctx => B =
171171
ctx => f(r(ctx))(ctx)
172172
def pure[A](x: A): Ctx => A =
173173
ctx => x
@@ -178,7 +178,7 @@ instance ReaderMonad[Ctx] of Monad[[X] => Ctx => X] {
178178

179179
Here is the new syntax of instance definitions, seen as a delta from the [standard context free syntax of Scala 3](http://dotty.epfl.ch/docs/internals/syntax.html).
180180
```
181-
TmplDef ::= ...
181+
TmplDef ::= ...
182182
| ‘instance’ InstanceDef
183183
InstanceDef ::= [id] InstanceParams [‘of’ ConstrApps] [TemplateBody]
184184
InstanceParams ::= [DefTypeParamClause] {‘with’ ‘(’ [DefParams] ‘)}

0 commit comments

Comments
 (0)