Skip to content

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

Merged
merged 1 commit into from
May 12, 2017

Conversation

travissarles
Copy link
Contributor

No description provided.

@travissarles travissarles mentioned this pull request Apr 5, 2017
33 tasks
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.
Copy link
Member

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)
Copy link
Member

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.
Copy link
Member

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.

Copy link
Member

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...?

Copy link
Contributor Author

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.

@travissarles travissarles force-pushed the by-name-params branch 2 times, most recently from 9f48bbf to 9f8df37 Compare April 10, 2017 14:17
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.
Copy link
Contributor

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:
Copy link
Contributor

Choose a reason for hiding this comment

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

Where is it?

Copy link
Contributor Author

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.

@travissarles travissarles merged commit d915d25 into scala:master May 12, 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.

3 participants