You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/docs/reference/witnesses/witnesses.md
+21-24
Original file line number
Diff line number
Diff line change
@@ -7,18 +7,18 @@ Witnesses provide a concise and uniform syntax for defining implicit values. Exa
7
7
8
8
```scala
9
9
traitOrd[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
+
defcompareTo(thisx: T)(y: T):Int
11
+
def< (thisx: T)(y: T) = x.compareTo(y) <0
12
+
def> (thisx: T)(y: T) = x.compareTo(y) >0
13
13
}
14
14
15
15
witness IntOrdforOrd[Int] {
16
-
def(x: Int) compareTo (y: Int) =
16
+
defcompareTo(thisx: Int)(y: Int) =
17
17
if (x < y) -1elseif (x > y) +1else0
18
18
}
19
19
20
20
witness ListOrd[T:Ord] forOrd[List[T]] {
21
-
def(xs: List[T]) compareTo (ys: List[T]):Int= (xs, ys) match {
21
+
defcompareTo(thisxs: List[T])(ys: List[T]):Int= (xs, ys) match {
22
22
case (Nil, Nil) =>0
23
23
case (Nil, _) =>-1
24
24
case (_, Nil) =>+1
@@ -32,12 +32,12 @@ witness ListOrd[T: Ord] for Ord[List[T]] {
32
32
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:
33
33
```scala
34
34
implicitobjectIntOrdextendsOrd[Int] {
35
-
def(x: Int) compareTo (y: Int) =
35
+
defcompareTo(thisx: Int)(y: Int) =
36
36
if (x < y) -1elseif (x > y) +1else0
37
37
}
38
38
39
39
classListOrd[T:Ord] extendsOrd[List[T]] {
40
-
def(xs: List[T]) compareTo (ys: List[T]):Int= (xs, ys) match {
40
+
defcompareTo(thisxs: List[T])(ys: List[T]):Int= (xs, ys) match {
41
41
case (Nil, Nil) =>0
42
42
case (Nil, _) =>-1
43
43
case (_, Nil) =>+1
@@ -60,14 +60,14 @@ Witnesses can also be defined without a `for` clause. A typical application is t
0 commit comments