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
Qualquer método que tenha um único parâmetro pode ser usado como um *operador infix* em Scala. Aqui está a definição da classe `MyBool` que inclui os métodos `add` e `or`:
15
+
16
+
```tut
17
+
case class MyBool(x: Boolean) {
18
+
def and(that: MyBool): MyBool = if (x) that else this
19
+
def or(that: MyBool): MyBool = if (x) this else that
20
+
def negate: MyBool = MyBool(!x)
21
+
}
22
+
```
23
+
24
+
Agora é possível utilizar as funções `and` and `or` como operadores infix:
25
+
26
+
```tut
27
+
def not(x: MyBool) = x.negate
28
+
def xor(x: MyBool, y: MyBool) = (x or y) and not(x and y)
29
+
```
30
+
31
+
Isso ajuda a tornar a definição de `xor` mais legível.
32
+
33
+
Aqui está o código correspondente em uma sintaxe de linguagem de programação orientada a objetos mais tradicional:
0 commit comments