Skip to content

Commit 4d616bb

Browse files
committed
Add code tabs for _tour/operators
1 parent de06e87 commit 4d616bb

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

_tour/operators.md

Lines changed: 63 additions & 0 deletions
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,27 +47,62 @@ 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

56101
## Precedence
57102
When an expression uses multiple operators, the operators are evaluated based on the priority of the first character:
103+
104+
{% tabs operators_6 %}
105+
{% tab 'Scala 2 and 3' for=operators_6 %}
58106
```
59107
(characters not shown below)
60108
* / %
@@ -67,12 +115,27 @@ When an expression uses multiple operators, the operators are evaluated based on
67115
|
68116
(all letters, $, _)
69117
```
118+
{% endtab %}
119+
{% endtabs %}
120+
70121
This applies to functions you define. For example, the following expression:
122+
123+
{% tabs operators_7 %}
124+
{% tab 'Scala 2 and 3' for=operators_7 %}
71125
```
72126
a + b ^? c ?^ d less a ==> b | c
73127
```
128+
{% endtab %}
129+
{% endtabs %}
130+
74131
Is equivalent to
132+
133+
{% tabs operators_8 %}
134+
{% tab 'Scala 2 and 3' for=operators_8 %}
75135
```
76136
((a + b) ^? (c ?^ d)) less ((a ==> b) | c)
77137
```
138+
{% endtab %}
139+
{% endtabs %}
140+
78141
`?^` has the highest precedence because it starts with the character `?`. `+` has the second highest precedence, followed by `==>`, `^?`, `|`, and `less`.

0 commit comments

Comments
 (0)