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/instances/instance-defs.md
+21-21Lines changed: 21 additions & 21 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,18 +7,18 @@ Instance definitions provide a concise and uniform syntax for defining implicit
7
7
8
8
```scala
9
9
traitOrd[T] {
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
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
13
13
}
14
14
15
15
instance IntOrd of Ord[Int] {
16
-
defcompareTo(thisx: Int)(y: Int) =
16
+
def(x: Int) compareTo (y: Int) =
17
17
if (x < y) -1elseif (x > y) +1else0
18
18
}
19
19
20
20
instance ListOrd[T:Ord] of Ord[List[T]] {
21
-
defcompareTo(thisxs: List[T])(ys: List[T]):Int= (xs, ys) match {
21
+
def(xs: List[T]) compareTo (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 @@ instance ListOrd[T: Ord] of Ord[List[T]] {
32
32
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:
33
33
```scala
34
34
implicitobjectIntOrdextendsOrd[Int] {
35
-
defcompareTo(thisx: Int)(y: Int) =
35
+
def(x: Int) compareTo (y: Int) =
36
36
if (x < y) -1elseif (x > y) +1else0
37
37
}
38
38
39
39
classListOrd[T:Ord] extendsOrd[List[T]] {
40
-
defcompareTo(thisxs: List[T])(ys: List[T]):Int= (xs, ys) match {
40
+
def(xs: List[T]) compareTo (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 @@ Instances can also be defined without an `of` clause. A typical application is t
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).
0 commit comments