Skip to content

Simplify Chinese translation of Scala Tour: by-name-parameters.md #1208

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 2 commits into from
Nov 30, 2018
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
29 changes: 28 additions & 1 deletion _zh-cn/tour/by-name-parameters.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
layout: tour
title: By-name Parameters
title: 传名参数

discourse: false

Expand All @@ -13,3 +13,30 @@ language: zh-cn
next-page: annotations
previous-page: operators
---

_传名参数_ 仅在被使用时触发实际参数的求值运算。 它们与 _传值参数_ 正好相反。 要将一个参数变为传名参数,只需在它的类型前加上 `=>`。
```tut
def calculate(input: => Int) = input * 37
```
传名参数的优点是,如果它们在函数体中未被使用,则不会对它们进行求值。 另一方面,传值参数的优点是它们仅被计算一次。
以下是我们如何实现一个 while 循环的例子:

```tut
def whileLoop(condition: => Boolean)(body: => Unit): Unit =
if (condition) {
body
whileLoop(condition)(body)
}

var i = 2

whileLoop (i > 0) {
println(i)
i -= 1
} // prints 2 1
```
方法 `whileLoop` 使用多个参数列表来分别获取循环条件和循环体。 如果 `condition` 为 true,则执行 `body`,然后对 whileLoop 进行递归调用。 如果 `condition` 为 false,则永远不会计算 body,因为我们在 `body` 的类型前加上了 `=>`。

现在当我们传递 `i > 0` 作为我们的 `condition` 并且 `println(i); i-= 1` 作为 `body` 时,它表现得像许多语言中的标准 while 循环。

如果参数是计算密集型或长时间运行的代码块,如获取 URL,这种延迟计算参数直到它被使用时才计算的能力可以帮助提高性能。