forked from scala/scala3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinfix.scala
68 lines (56 loc) · 1.57 KB
/
infix.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Compile with -strict -Xfatal-warnings -deprecation
class C:
infix def op(x: Int): Int = ???
def meth(x: Int): Int = ???
def matching(x: Int => Int) = ???
def +(x: Int): Int = ???
object C:
given AnyRef with
extension (x: C)
infix def iop (y: Int) = ???
def mop (y: Int) = ???
def ++ (y: Int) = ???
val c = C()
def test() = {
c op 2
c iop 2
c.meth(2)
c ++ 2
c.op(2)
c.iop(2)
c mop 2 // error: should not be used as infix operator
c meth 2 // error: should not be used as infix operator
c `meth` 2 // OK, sincd `meth` is backquoted
c + 3 // OK, since `+` is symbolic
1 to 2 // OK, since `to` is defined by Scala-2
c meth { // OK, since `meth` is followed by `{...}`
3
}
c matching { // OK, since `meth` is followed by `{...}`
case x => x
}
infix class Or[X, Y]
class AndC[X, Y]
infix type And[X, Y] = AndC[X, Y]
infix type &&[X, Y] = AndC[X, Y]
class Map[X, Y]
val x1: Int Map String = ??? // error
val x2: Int Or String = ??? // OK since Or is declared `infix`
val x3: Int AndC String = ??? // error
val x4: Int `AndC` String = ??? // OK
val x5: Int And String = ??? // OK
val x6: Int && String = ???
case class Pair[T](x: T, y: T)
infix case class Q[T](x: T, y: T)
object PP {
infix def unapply[T](x: Pair[T]): Option[(T, T)] = Some((x.x, x.y))
}
val p = Pair(1, 2)
val Pair(_, _) = p
val _ Pair _ = p // error
val _ `Pair` _ = p // OK
val (_ PP _) = p: @unchecked // OK
val q = Q(1, 2)
val Q(_, _) = q
val _ Q _ = q // OK
}