Skip to content

Commit 866439c

Browse files
committed
Tutorials - Translation PT-BR - Operators
1 parent fe50614 commit 866439c

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

pt-br/tutorials/tour/operators.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
---
2+
layout: tutorial
3+
title: Operadores
4+
5+
disqus: true
6+
7+
tutorial: scala-tour
8+
num: 29
9+
tutorial-next: automatic-closures
10+
tutorial-previous: local-type-inference
11+
language: pt-br
12+
---
13+
14+
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:
34+
35+
```tut
36+
def not(x: MyBool) = x.negate
37+
def xor(x: MyBool, y: MyBool) = x.or(y).and(x.and(y).negate)
38+
```

0 commit comments

Comments
 (0)