Skip to content

Rewrote operators #707

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 10, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 47 additions & 6 deletions tutorials/tour/_posts/2017-02-13-operators.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,33 @@ categories: tour
num: 30
next-page: automatic-closures
previous-page: local-type-inference
prerequisite-knowledge: case-classes
---
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:
```
10.+(1)
```

However, it's easier to read as an infix operator:
```
10 + 1
```

## Defining and using operators
You can use any legal identifier as an operator. This includes a name like `add` or a symbol(s) like `+`.
```tut
case class Vec(val x: Double, val y: Double) {
def +(that: Vec) = new Vec(this.x + that.x, this.y + that.y)
}

val vector1 = Vec(1.0, 1.0)
val vector2 = Vec(2.0, 2.0)

Any method which takes a single parameter can be used as an *infix operator* in Scala. Here is the definition of class `MyBool` which includes methods `and` and `or`:
val vector3 = vector1 + vector2
vector3.x // 3.0
vector3.y // 3.0
```
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`:

```tut
case class MyBool(x: Boolean) {
Expand All @@ -30,9 +54,26 @@ def xor(x: MyBool, y: MyBool) = (x or y) and not(x and y)

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

Here is the corresponding code in a more traditional object-oriented programming language syntax:

```tut
def not(x: MyBool) = x.negate
def xor(x: MyBool, y: MyBool) = x.or(y).and(x.and(y).negate)
## Precedence
When an expression uses multiple operators, the operators are evaluated based on the priority of the first character:
```
(characters not shown below)
* / %
+ -
:
= !
< >
&
^
|
(all letters)
```
This applies to functions you define. For example, the following expression:
```
a + b ^? c ?^ d less a ==> b | c
```
Is equivalent to
```
((a + b) ^? (c ?^ d)) less ((a ==> b) | c)
```
`?^` has the highest precedence because it starts with the character `?`. `+` has the second highest precedence, followed by `^?`, `==>`, `|`, and `less`.
Copy link

@MasseGuillaume MasseGuillaume Feb 23, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we are missing the suffix :

for example:

case class Foo(v: Int) {
  def <>:(x: Int): Foo = Foo(v + x)
}

1 <>: Foo(2)

is equivalent to Foo(2).<>:(1)

a notable example is val a = 1 :: 2 :: 3 :: Nil