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

Rewrote operators #707

merged 1 commit into from
May 10, 2017

Conversation

travissarles
Copy link
Contributor

No description provided.

vector3.x // 3.0
vector3.y // 3.0
```
The class vector has a method `+` which we used to add vector1 and vector2. When the method has two or more parameters, use a tuple:
Copy link

Choose a reason for hiding this comment

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

This isn't actually a tuple, just a consequence of point-free notation: a.b(c) becomes a b c, but a.b(c, d) becomes a b (c, d).

Copy link
Contributor

Choose a reason for hiding this comment

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

I don’t think it is worth mentioning this special case. In practice, infix notation is mostly used with methods that take one parameter.

def not(x: MyBool) = x.negate
def xor(x: MyBool, y: MyBool) = x.or(y).and(x.and(y).negate)
```
This helps to make the definition of `xor` more readable.If the method takes more than one argument, use a tuple:
Copy link

Choose a reason for hiding this comment

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

You seem to have stray text on the end here.


You can also give `+` specific meaning in your class:
```tut
class Vector(val x: Double, val y: Double) {
Copy link

Choose a reason for hiding this comment

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

Why don't you call this Vec to avoid collision with scala.collection.immutable.Vector (whose name is really unfortunate for anyone thinking about mathematical vectors)?

10.+(1)
```

However, it more closely resembles math if you write it as an infix operator:
Copy link

Choose a reason for hiding this comment

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

I don't think "closely resembles math" is quite what you mean. It is math. It more closely resembles classical mathematical notation for addition, but that's quite a mouthful.

Maybe something like, "However, the expression is easier to recognize when written in a familiar form with + as an operator:". (I don't think that wording is ideal either, actually.)

@@ -10,8 +10,40 @@ num: 30
next-page: automatic-closures
previous-page: local-type-inference
---
In Scala, operators are methods. For example, `+` can be called with dot-notation:
Copy link

Choose a reason for hiding this comment

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

You never actually say that methods can be used as operators! This is the most important piece of information in this section! Please add it again!

10 + 1
```

You can also give `+` specific meaning in your class:
Copy link
Contributor

Choose a reason for hiding this comment

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

I would say “You can define your own operators”

vector3.x // 3.0
vector3.y // 3.0
```
The class vector has a method `+` which we used to add vector1 and vector2. When the method has two or more parameters, use a tuple:
Copy link
Contributor

Choose a reason for hiding this comment

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

I don’t think it is worth mentioning this special case. In practice, infix notation is mostly used with methods that take one parameter.


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`:
Using parentheses, you can build up complex expressions with readable syntax. Here is the definition of class `MyBool` which includes methods `and` and `or`:
Copy link
Contributor

Choose a reason for hiding this comment

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

You could maybe stay in the same Vec context and use add instead of + and say that all methods (not just those with symbolic names) can be used with the infix notation.

@julienrf
Copy link
Contributor

Also, what do you think of giving some words, at the end, about associativity and precedence resolution?

## 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
class Vec(val x: Double, val y: Double) {

Choose a reason for hiding this comment

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

case class

def +(that: Vec) = new Vec(this.x + that.x, this.y + that.y)
}

val vector1 = new Vec(1.0, 1.0)

Choose a reason for hiding this comment

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

just Vec

```
`?^` 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

@travissarles travissarles mentioned this pull request Mar 17, 2017
33 tasks
@travissarles travissarles merged commit 5891b03 into scala:master May 10, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants