-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Rewrote operators #707
Conversation
27b4189
to
f73d84d
Compare
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: |
There was a problem hiding this comment.
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)
.
There was a problem hiding this comment.
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: |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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: |
There was a problem hiding this comment.
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: |
There was a problem hiding this comment.
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: |
There was a problem hiding this comment.
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: |
There was a problem hiding this comment.
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`: |
There was a problem hiding this comment.
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.
Also, what do you think of giving some words, at the end, about associativity and precedence resolution? |
f73d84d
to
f7bd4c3
Compare
## 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) { |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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`. |
There was a problem hiding this comment.
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
No description provided.