Skip to content

Commit a05717e

Browse files
committed
Revert alternative infix syntax for extension methods
That one was slipped in by accident.
1 parent 03f1021 commit a05717e

File tree

1 file changed

+21
-24
lines changed

1 file changed

+21
-24
lines changed

docs/docs/reference/witnesses/witnesses.md

+21-24
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,18 @@ Witnesses provide a concise and uniform syntax for defining implicit values. Exa
77

88
```scala
99
trait Ord[T] {
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
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
1313
}
1414

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

2020
witness ListOrd[T: Ord] for Ord[List[T]] {
21-
def (xs: List[T]) compareTo (ys: List[T]): Int = (xs, ys) match {
21+
def compareTo(this xs: List[T])(ys: List[T]): Int = (xs, ys) match {
2222
case (Nil, Nil) => 0
2323
case (Nil, _) => -1
2424
case (_, Nil) => +1
@@ -32,12 +32,12 @@ witness ListOrd[T: Ord] for Ord[List[T]] {
3232
Witness can be seen as shorthands for what is currently expressed as implicit definitions. The witnesses above could also have been formulated as implicits as follows:
3333
```scala
3434
implicit object IntOrd extends Ord[Int] {
35-
def (x: Int) compareTo (y: Int) =
35+
def compareTo(this x: Int)(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 (xs: List[T]) compareTo (ys: List[T]): Int = (xs, ys) match {
40+
def compareTo(this xs: List[T])(ys: List[T]): Int = (xs, ys) match {
4141
case (Nil, Nil) => 0
4242
case (Nil, _) => -1
4343
case (_, Nil) => +1
@@ -60,14 +60,14 @@ Witnesses can also be defined without a `for` clause. A typical application is t
6060

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

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

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

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

@@ -105,7 +105,7 @@ The `with` clause in a witness defines required witnesses. The witness for `Conv
105105
```scala
106106
class Convertible_List_List_witness[From, To](implicit c: Convertible[From, To])
107107
extends Convertible[List[From], List[To]] {
108-
def (x: List[From]) convert: List[To] = x.map(c.convert)
108+
def convert (this x: List[From]): List[To] = x.map(c.convert)
109109
}
110110
implicit def Convertible_List_List_witness[From, To](implicit c: Convertible[From, To])
111111
: Convertible[List[From], List[To]] =
@@ -132,45 +132,42 @@ Semigroups and monoids:
132132

133133
```scala
134134
trait SemiGroup[T] {
135-
def (x: T) combine (y: T): T
135+
def combine(this x: T)(y: T): T
136136
}
137137
trait Monoid[T] extends SemiGroup[T] {
138138
def unit: T
139139
}
140-
object Monoid {
141-
def apply[T: Monoid] = summon[Monoid[T]]
142-
}
143140

144141
witness for Monoid[String] {
145-
def (x: String) combine (y: String): String = x.concat(y)
142+
def combine(this x: String)(y: String): String = x.concat(y)
146143
def unit: String = ""
147144
}
148145

149146
def sum[T: Monoid](xs: List[T]): T =
150-
xs.foldLeft(Monoid[T].unit)(_.combine(_))
147+
xs.foldLeft(summon[Monoid[T]].unit)(_.combine(_))
151148
```
152149
Functors and monads:
153150
```scala
154151
trait Functor[F[_]] {
155-
def (x: F[A]) map[A, B] (f: A => B): F[B]
152+
def map[A, B](this x: F[A])(f: A => B): F[B]
156153
}
157154

158155
trait Monad[F[_]] extends Functor[F] {
159-
def (x: F[A]) flatMap[A, B] (f: A => F[B]): F[B]
160-
def (x: F[A]) map[A, B] (f: A => B) = x.flatMap(f `andThen` pure)
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)
161158

162159
def pure[A](x: A): F[A]
163160
}
164161

165162
witness ListMonad for Monad[List] {
166-
def (xs: List[A]) flatMap[A, B] (f: A => List[B]): List[B] =
163+
def flatMap[A, B](this xs: List[A])(f: A => List[B]): List[B] =
167164
xs.flatMap(f)
168165
def pure[A](x: A): List[A] =
169166
List(x)
170167
}
171168

172169
witness ReaderMonad[Ctx] for Monad[[X] => Ctx => X] {
173-
def (r: Ctx => A) flatMap[A, B] (f: A => Ctx => B): Ctx => B =
170+
def flatMap[A, B](this r: Ctx => A)(f: A => Ctx => B): Ctx => B =
174171
ctx => f(r(ctx))(ctx)
175172
def pure[A](x: A): Ctx => A =
176173
ctx => x

0 commit comments

Comments
 (0)