Skip to content

Commit cf34d4a

Browse files
authored
Merge pull request #2561 from flomebul/operators
Add code tabs for _tour/operators
2 parents f0b9269 + 1c7d8be commit cf34d4a

File tree

1 file changed

+58
-1
lines changed

1 file changed

+58
-1
lines changed

_tour/operators.md

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,30 @@ prerequisite-knowledge: case-classes
1111
redirect_from: "/tutorials/tour/operators.html"
1212
---
1313
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 %}
1417
```
1518
10.+(1)
1619
```
20+
{% endtab %}
21+
{% endtabs %}
1722

1823
However, it's easier to read as an infix operator:
24+
25+
{% tabs operators_2 %}
26+
{% tab 'Scala 2 and 3' for=operators_2 %}
1927
```
2028
10 + 1
2129
```
30+
{% endtab %}
31+
{% endtabs %}
2232

2333
## Defining and using operators
2434
You can use any legal identifier as an operator. This includes a name like `add` or a symbol(s) like `+`.
35+
36+
{% tabs operators_3 class=tabs-scala-version %}
37+
{% tab 'Scala 2' for=operators_3 %}
2538
```scala mdoc
2639
case class Vec(x: Double, y: Double) {
2740
def +(that: Vec) = Vec(this.x + that.x, this.y + that.y)
@@ -34,22 +47,54 @@ val vector3 = vector1 + vector2
3447
vector3.x // 3.0
3548
vector3.y // 3.0
3649
```
50+
{% endtab %}
51+
{% tab 'Scala 3' for=operators_3 %}
52+
```scala
53+
case class Vec(x: Double, y: Double):
54+
def +(that: Vec) = Vec(this.x + that.x, this.y + that.y)
55+
56+
val vector1 = Vec(1.0, 1.0)
57+
val vector2 = Vec(2.0, 2.0)
58+
59+
val vector3 = vector1 + vector2
60+
vector3.x // 3.0
61+
vector3.y // 3.0
62+
```
63+
{% endtab %}
64+
{% endtabs %}
65+
3766
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`:
3867

68+
{% tabs operators_4 class=tabs-scala-version %}
69+
{% tab 'Scala 2' for=operators_4 %}
3970
```scala mdoc
4071
case class MyBool(x: Boolean) {
4172
def and(that: MyBool): MyBool = if (x) that else this
4273
def or(that: MyBool): MyBool = if (x) this else that
4374
def negate: MyBool = MyBool(!x)
4475
}
4576
```
77+
{% endtab %}
78+
{% tab 'Scala 3' for=operators_4 %}
79+
```scala
80+
case class MyBool(x: Boolean):
81+
def and(that: MyBool): MyBool = if x then that else this
82+
def or(that: MyBool): MyBool = if x then this else that
83+
def negate: MyBool = MyBool(!x)
84+
```
85+
{% endtab %}
86+
{% endtabs %}
4687

4788
It is now possible to use `and` and `or` as infix operators:
4889

90+
{% tabs operators_5 %}
91+
{% tab 'Scala 2 and 3' for=operators_5 %}
4992
```scala mdoc
5093
def not(x: MyBool) = x.negate
5194
def xor(x: MyBool, y: MyBool) = (x or y) and not(x and y)
5295
```
96+
{% endtab %}
97+
{% endtabs %}
5398

5499
This helps to make the definition of `xor` more readable.
55100

@@ -60,19 +105,31 @@ When an expression uses multiple operators, the operators are evaluated based on
60105
* / %
61106
+ -
62107
:
63-
= !
64108
< >
109+
= !
65110
&
66111
^
67112
|
68113
(all letters, $, _)
69114
```
70115
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 %}
71119
```
72120
a + b ^? c ?^ d less a ==> b | c
73121
```
122+
{% endtab %}
123+
{% endtabs %}
124+
74125
Is equivalent to
126+
127+
{% tabs operators_8 %}
128+
{% tab 'Scala 2 and 3' for=operators_8 %}
75129
```
76130
((a + b) ^? (c ?^ d)) less ((a ==> b) | c)
77131
```
132+
{% endtab %}
133+
{% endtabs %}
134+
78135
`?^` has the highest precedence because it starts with the character `?`. `+` has the second highest precedence, followed by `==>`, `^?`, `|`, and `less`.

0 commit comments

Comments
 (0)