Skip to content

Simplify Chinese translation of Scala Tour: type-inference.md #1207

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 3 commits into from
Nov 30, 2018

Conversation

realwunan
Copy link
Contributor

Simplify Chinese translation of Scala Tour: Type Inference

val q = id(1) // type: Int
```

编译器使用参数 `MyPair` 的类型来推断出 `A` 和 `B` 的类型。对于 `x` 的类型同样如此。
Copy link
Contributor

Choose a reason for hiding this comment

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

编译器使用传给MyPair参数的类型

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks a lot.


## 参数

编译器从不推断方法参数的类型。 但是,在某些情况下,当函数作为参数传递时,它可以推断出匿名函数参数类型。
Copy link
Contributor

Choose a reason for hiding this comment

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

方法参数 ->方法形式参数

Copy link
Contributor

Choose a reason for hiding this comment

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

编译器可以推断出匿名函数形式参数的类型

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks a lot.

Seq(1, 3, 4).map(x => x * 2) // List(2, 6, 8)
```

方法 map 的参数是 `f: A => B`。 因为我们把整数放在 `Seq` 中,编译器知道 `A` 是 `Int` 类型 (即 `x` 是一个整数)。 因此,编译器可以从 `x * 2` 推断出 `B` 是 `Int` 类型。
Copy link
Contributor

Choose a reason for hiding this comment

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

形式参数

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks a lot.


## 何时 _不要_ 依赖类型推断

通常认为声明在公共 API 中的成员类型更具可读性。 因此,我们建议你为将在你的代码中向用户公开的任何 API 明确指定类型。
Copy link
Contributor

Choose a reason for hiding this comment

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

通常认为,公开可访问的 API 成员应该具有显示类型声明。

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks a lot.

var obj = null
```

我们就不能继续重新分配值:
Copy link
Contributor

Choose a reason for hiding this comment

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

我们就不能进行重新赋值:

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's much better, thanks a lot.

Copy link
Contributor

@liufengyun liufengyun left a comment

Choose a reason for hiding this comment

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

Thanks a lot for the contribution @realwunan 🎉

@liufengyun liufengyun merged commit db4042a into scala:master Nov 30, 2018
@realwunan realwunan deleted the type-inference branch November 30, 2018 13:19
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.

2 participants