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
The result type of a alias witness is mandatory unless the witness definition
56
56
occurs as a statement in a block and lacks any type or value parameters. This corresponds to the same restriction for implicit vals in Dotty.
57
57
58
-
Abstract witnesses are equivalent to abstract implicit defs. Alias witnesses are equivalent to implicit defs if they are parameterized or for implicit vals otherwise. For instance, the witnesses defined so far in this section are equivalent to:
58
+
Abstract witnesses are equivalent to abstract implicit defs. Alias witnesses are equivalent to implicit defs if they are parameterized or have a `=> T` result type.
59
+
They translate to implicit vals otherwise. For instance, the witnesses defined so far in this section are equivalent to:
59
60
```scala
60
61
implicitdefsymDeco:SymDeco
61
62
implicitvalsymDeco:SymDeco= compilerSymOps
62
63
63
64
implicitvalctx= outer.ctx
64
65
implicitvalctx:Context= outer.ctx
65
-
implicitdefbyNameCtx():Ctx= outer.ctx
66
+
implicitdefbyNameCtx:Ctx= outer.ctx
66
67
implicitdeff[T]:C[T] =newC[T]
67
68
implicitdefg(implicitctx: Context):D=newD(ctx)
68
69
@@ -107,6 +108,7 @@ The syntax changes for this page are summarized as follows:
107
108
```
108
109
WitnessDef ::= ...
109
110
| id WitnessParams ‘:’ Type ‘=’ Expr
111
+
| id ‘:’ ‘=>’ Type ‘=’ Expr
110
112
| id ‘=’ Expr
111
113
```
112
114
In addition, the `implicit` modifier is removed together with all [productions]((http://dotty.epfl.ch/docs/internals/syntax.html) that reference it.
Copy file name to clipboardExpand all lines: docs/docs/reference/witnesses/witnesses.md
+24-21
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
-
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
witness IntOrdforOrd[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
witness ListOrd[T:Ord] forOrd[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 @@ 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
-
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 @@ Witnesses can also be defined without a `for` clause. A typical application is t
0 commit comments