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
In Scala, operators are methods. Any method with a single parameter can be used as an _infix operator_. For example, `+` can be called with dot-notation:
14
+
15
+
{% tabs operators_1 %}
16
+
{% tab 'Scala 2 and 3' for=operators_1 %}
14
17
```
15
18
10.+(1)
16
19
```
20
+
{% endtab %}
21
+
{% endtabs %}
17
22
18
23
However, it's easier to read as an infix operator:
24
+
25
+
{% tabs operators_2 %}
26
+
{% tab 'Scala 2 and 3' for=operators_2 %}
19
27
```
20
28
10 + 1
21
29
```
30
+
{% endtab %}
31
+
{% endtabs %}
22
32
23
33
## Defining and using operators
24
34
You can use any legal identifier as an operator. This includes a name like `add` or a symbol(s) like `+`.
The class Vec has a method `+` which we used to add `vector1` and `vector2`. Using parentheses, you can build up complex expressions with readable syntax. Here is the definition of class `MyBool` which includes methods `and` and `or`:
38
67
68
+
{% tabs operators_4 class=tabs-scala-version %}
69
+
{% tab 'Scala 2' for=operators_4 %}
39
70
```scala mdoc
40
71
caseclassMyBool(x: Boolean) {
41
72
defand(that: MyBool):MyBool=if (x) that elsethis
42
73
defor(that: MyBool):MyBool=if (x) thiselse that
43
74
defnegate:MyBool=MyBool(!x)
44
75
}
45
76
```
77
+
{% endtab %}
78
+
{% tab 'Scala 3' for=operators_4 %}
79
+
```scala
80
+
caseclassMyBool(x: Boolean):
81
+
defand(that: MyBool):MyBool=if x then that elsethis
82
+
defor(that: MyBool):MyBool=if x thenthiselse that
83
+
defnegate:MyBool=MyBool(!x)
84
+
```
85
+
{% endtab %}
86
+
{% endtabs %}
46
87
47
88
It is now possible to use `and` and `or` as infix operators:
48
89
90
+
{% tabs operators_5 %}
91
+
{% tab 'Scala 2 and 3' for=operators_5 %}
49
92
```scala mdoc
50
93
defnot(x: MyBool) = x.negate
51
94
defxor(x: MyBool, y: MyBool) = (x or y) and not(x and y)
52
95
```
96
+
{% endtab %}
97
+
{% endtabs %}
53
98
54
99
This helps to make the definition of `xor` more readable.
55
100
@@ -60,19 +105,31 @@ When an expression uses multiple operators, the operators are evaluated based on
60
105
* / %
61
106
+ -
62
107
:
63
-
= !
64
108
< >
109
+
= !
65
110
&
66
111
^
67
112
|
68
113
(all letters, $, _)
69
114
```
70
115
This applies to functions you define. For example, the following expression:
116
+
117
+
{% tabs operators_7 %}
118
+
{% tab 'Scala 2 and 3' for=operators_7 %}
71
119
```
72
120
a + b ^? c ?^ d less a ==> b | c
73
121
```
122
+
{% endtab %}
123
+
{% endtabs %}
124
+
74
125
Is equivalent to
126
+
127
+
{% tabs operators_8 %}
128
+
{% tab 'Scala 2 and 3' for=operators_8 %}
75
129
```
76
130
((a + b) ^? (c ?^ d)) less ((a ==> b) | c)
77
131
```
132
+
{% endtab %}
133
+
{% endtabs %}
134
+
78
135
`?^` has the highest precedence because it starts with the character `?`. `+` has the second highest precedence, followed by `==>`, `^?`, `|`, and `less`.
0 commit comments