-
Notifications
You must be signed in to change notification settings - Fork 1k
Rewrote by-name parameters section of tour #757
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
Conversation
previous-page: operators | ||
--- | ||
|
||
_By-name parameters_ are only evaluated when used. They are in contrast to _by-value parameters_. To make a parameter called by value, simply prepend `=>` to its type. |
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.
"To make a parameter called by name", you mean
_By-name parameters_ are only evaluated when used. They are in contrast to _by-value parameters_. To make a parameter called by value, simply prepend `=>` to its type. | ||
|
||
```tut | ||
def goToSleep(tired: Boolean, seconds: => Int) = if (tired) Thread.sleep(seconds) |
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.
too long for a one-liner, especially given the site layout
def lengthOfNap = 3600 * 8 | ||
goToSleep(tired = false, seconds = lengthOfNap) | ||
``` | ||
The type of `seconds` is `=> Int` which means `seconds` is a by-name parameter. Therefore, when we call `goToSleep` it with `tired = false`, `lengthOfNap` will not be evaluated. |
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 is not that compelling of an example. I would suggest leading with an example where avoiding unwanted evaluation is actually crucial. Computing a number of seconds is highly unlikely to be an expensive computation.
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.
What did you see as the downsides with the examples in the original version...?
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.
Looking back at the original example, it does look better. I'll put it back.
9f48bbf
to
9f8df37
Compare
i -= 1 | ||
} // prints 2 1 | ||
``` | ||
The method `whileLoop` uses multiple parameter lists to take a condition and a body of the loop. If the `condition` is true, the `body` is executed and then a recursive call to whileLoop is made. If the `condition` is false, the body is never evaluated because we prepended `=>` to the return type. |
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.
=>
wasn't appended to the return type; it was appended to body
's type.
``` | ||
The method `whileLoop` uses multiple parameter lists to take a condition and a body of the loop. If the `condition` is true, the `body` is executed and then a recursive call to whileLoop is made. If the `condition` is false, the body is never evaluated because we prepended `=>` to the return type. | ||
|
||
Now when we pass `i > 0` as our `condition` and `println(i); i-= 1` as the `body`, it behaves like the standard while loop in many languages. Here is the output: |
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.
Where is it?
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'll remove that sentence since I have the output in a comment.
9f8df37
to
af711b6
Compare
No description provided.