Skip to content

Commit f256b1e

Browse files
authored
Merge pull request #2394 from benluo/main
2 parents e1c54ad + ba6db33 commit f256b1e

File tree

78 files changed

+14921
-21
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+14921
-21
lines changed

_zh-cn/index.md

+13-9
Original file line numberDiff line numberDiff line change
@@ -12,37 +12,37 @@ scala3-sections:
1212
- title: "第一步"
1313
links:
1414
- title: "Scala 3 中的新东西"
15-
description: "Scala 3 现存新特性概览"
15+
description: "Scala 3 中令人兴奋的新特性概览"
1616
icon: "fa fa-star"
1717
link: /scala3/new-in-scala3.html
1818
- title: "快速开始"
19-
description: "安装 Scala 3 并开始写些 Scala 代码"
19+
description: "在你的电脑中安装 Scala 3,然后开始写些 Scala 代码"
2020
icon: "fa fa-rocket"
2121
link: /scala3/getting-started.html
22-
- title: "Scala 3 书籍"
23-
description: "一部介绍主要语言特性的线上书"
22+
- title: "Scala 3 册子"
23+
description: "通过一系列小课程来学习 Scala。"
2424
icon: "fa fa-book"
2525
link: /scala3/book/introduction.html
2626
- title: "更多细节"
2727
links:
2828
- title: "迁移指引"
29-
description: "一份帮你从 Scala 2 迁移到 Scala 3 的指引"
29+
description: "一份帮你从 Scala 2 迁移到 Scala 3 的指引"
3030
icon: "fa fa-suitcase"
3131
link: /scala3/guides/migration/compatibility-intro.html
3232
- title: "导览"
33-
description: "关于语言特别之处的详细导览"
33+
description: "关于语言特别之处的详细导览"
3434
icon: "fa fa-map"
3535
link: /zh-cn/scala3/guides.html
3636
- title: "Scala 库 API"
37-
description: "Scala 3 标准库API文档(多个小版本)"
37+
description: "Scala 3 每一个版本的标准库API文档。"
3838
icon: "fa fa-file-alt"
3939
link: https://scala-lang.org/api/3.x/
4040
- title: "语言参考手册"
41-
description: "Scala 3 语言参考手册"
41+
description: "Scala 3 语言参考手册"
4242
icon: "fa fa-book"
4343
link: https://docs.scala-lang.org/scala3/reference
4444
- title: "贡献指南"
45-
description: "Scala 3 编译器指南及如何贡献代码"
45+
description: "Scala 3 编译器指南及如何修复问题"
4646
icon: "fa fa-cogs"
4747
link: /scala3/guides/contribution/contribution-intro.html
4848
- title: "Scala 3 全新的 Scaladoc"
@@ -109,4 +109,8 @@ scala2-sections:
109109
description: "Scala改进过程(Scala Improvement Process),语言及编译器进展"
110110
icon: "fa fa-cogs"
111111
link: /sips/index.html
112+
- title: "为 Scala 作贡献"
113+
description: "为 Scala 项目作贡献的完整指南"
114+
icon: "fa fa-cogs"
115+
link: /contribute/
112116
---
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
---
2+
title: 上下文绑定
3+
type: section
4+
description: This page demonstrates Context Bounds in Scala 3.
5+
num: 61
6+
previous-page: types-type-classes
7+
next-page: ca-given-imports
8+
---
9+
10+
11+
{% comment %}
12+
- TODO: define "context parameter"
13+
- TODO: define "synthesized" and "synthesized arguments"
14+
{% endcomment %}
15+
16+
17+
在许多情况下,_上下文参数_的名称不必明确提及,因为它仅由编译器在其他上下文参数的合成参数中使用。
18+
在这种情况下,您不必定义参数名称,只需提供参数类型即可。
19+
20+
## 背景
21+
22+
例如,这个 `maximum` 方法接受 `Ord` 类型的_上下文参数_,只是将它作为参数传递给 `max`
23+
24+
```scala
25+
def maximum[A](xs: List[A])(using ord: Ord[A]): A =
26+
xs.reduceLeft(max(ord))
27+
```
28+
29+
在该代码中,参数名称 `ord` 实际上不是必需的;它可以作为推断参数传递给 `max`,因此您只需声明 `maximum` 使用的类型 `Ord[A]` 而不必给它命名:
30+
31+
```scala
32+
def maximum[A](xs: List[A])(using Ord[A]): A =
33+
xs.reduceLeft(max)
34+
```
35+
36+
## 上下文绑定
37+
38+
鉴于此背景,_上下文绑定_是一种简写语法,用于表达“依赖于类型参数的上下文参数”模式。
39+
40+
使用上下文绑定,`maximum` 方法可以这样写:
41+
42+
```scala
43+
def maximum[A: Ord](xs: List[A]): A = xs.reduceLeft(max)
44+
```
45+
46+
方法或类的类型参数 `A`,有类似 `:Ord` 的绑定,它表示有 `Ord[A]` 的上下文参数。
47+
48+
有关上下文绑定的更多信息,请参阅 Scala 常见问题解答的 [“什么是上下文绑定?”](https://docs.scala-lang.org/tutorials/FAQ/context-bounds.html) 部分。
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
---
2+
title: 上下文抽象
3+
type: chapter
4+
description: This chapter provides an introduction to the Scala 3 concept of Contextual Abstractions.
5+
num: 58
6+
previous-page: types-others
7+
next-page: ca-given-using-clauses
8+
---
9+
10+
11+
## 背景
12+
13+
隐式是Scala 2中的一个主要的设计特色。隐式是进行上下文抽象的基本方式。它们代表了具有多种用例的统一范式,其中包括:
14+
15+
- 实现类型类
16+
- 建立上下文
17+
- 依赖注入
18+
- 表达能力
19+
- 计算新类型,并证明它们之间的关系
20+
21+
此后,其他语言也纷纷效仿,例如 Rust 的 traits 或 Swift 的协议扩展。对于编译期的依赖解析方案,Kotlin 的设计方案也被提上日程,而对 C# 而言是 Shapes 和 Extensions 或对 F# 来说是 Traits。Implicits 也是 Coq 或 Agda 等定理证明器的共同特色。
22+
23+
尽管这些设计使用不同的术语,但它们都是*术语推理*核心思想的变体:
24+
给定一个类型,编译器会合成一个具有该类型的“canonical”术语。
25+
26+
## 重新设计
27+
28+
Scala 3 重新设计了 Scala 中的上下文抽象。
29+
虽然这些概念是在 Scala 2 中逐渐“发现”的,但它们现在已广为人知和理解,并且重新设计利用了这些知识。
30+
31+
Scala 3 的设计侧重于 **意图** 而不是 **机制**
32+
Scala 3 没有提供一个非常强大的隐式特性,而是提供了几个面向用例的特性:
33+
34+
- **抽象上下文信息**
35+
[使用子句][givens] 允许程序员对调用上下文中可用的信息进行抽象,并且应该隐式传递。
36+
作为对 Scala 2 隐式的改进,可以按类型指定 using 子句,从而将函数签名从从未显式引用的术语变量名称中释放出来。
37+
38+
- **提供类型类实例**
39+
[给定实例][type-classes]允许程序员定义某种类型的_规范值(canonical value)_
40+
这使得使用类型类的编程更加简单,而不会泄露实现细节。
41+
42+
- **追溯扩展类**
43+
在 Scala 2 中,扩展方法必须使用隐式转换或隐式类进行编码。
44+
相比之下,在 Scala 3 [扩展方法][extension-methods] 现在直接内置到语言中,产生了更好的错误消息和改进的类型推断。
45+
46+
- **将一种类型视为另一种类型**
47+
隐式转换已从头开始[重新设计][implicit-conversions],作为类型类 `Conversion` 的实例。
48+
49+
- **高阶上下文抽象**
50+
[上下文函数][contextual-functions] 的_全新_特性使上下文抽象成为一等公民。
51+
它们是库作者的重要工具,可以表达简洁的领域特定语言。
52+
53+
- **来自编译器的可操作反馈**
54+
如果编译器无法解析隐式参数,它现在会为您提供 [导入建议](https://www.scala-lang.org/blog/2020/05/05/scala-3-import-suggestions.html) 来解决问题。
55+
56+
## 好处
57+
58+
Scala 3 中的这些更改实现了术语推理与语言其余部分的更好分离:
59+
60+
- 仅有一种定义 given 的方法
61+
- 仅有一种方法可以引入隐式参数和自变量
62+
- [导入 givens][given-imports] 仅有一种单独的方法,不允许它们隐藏在正常导入的海洋中
63+
- 定义 [隐式转换][implicit-conversions] 的方法仅有一种,它被清楚地标记为这样,并且不需要特殊的语法
64+
65+
这些变化的好处包括:
66+
67+
- 新设计避免了特性交织,从而使语言更加一致
68+
- 它使隐式更容易学习和更难滥用
69+
- 它极大地提高了 95% 使用隐式的 Scala 程序的清晰度
70+
- 它有可能以一种易于理解和友好的原则方式进行术语推理
71+
72+
本章将在以下各节中介绍其中的许多新功能。
73+
74+
75+
[givens]: {% link _overviews/scala3-book/ca-given-using-clauses.md %}
76+
[given-imports]: {% link _overviews/scala3-book/ca-given-imports.md %}
77+
[implicit-conversions]: {% link _overviews/scala3-book/ca-implicit-conversions.md %}
78+
[extension-methods]: {% link _overviews/scala3-book/ca-extension-methods.md %}
79+
[context-bounds]: {% link _overviews/scala3-book/ca-context-bounds.md %}
80+
[type-classes]: {% link _overviews/scala3-book/ca-type-classes.md %}
81+
[equality]: {% link _overviews/scala3-book/ca-multiversal-equality.md %}
82+
[contextual-functions]: {% link _overviews/scala3-book/types-dependent-function.md %}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
---
2+
title: 扩展方法
3+
type: section
4+
description: This page demonstrates how Extension Methods work in Scala 3.
5+
num: 63
6+
previous-page: ca-given-imports
7+
next-page: ca-type-classes
8+
---
9+
10+
11+
扩展方法允许您在定义类型后向类型添加方法,即它们允许您向封闭类添加新方法。
12+
例如,假设其他人创建了一个 `Circle` 类:
13+
14+
```scala
15+
case class Circle(x: Double, y: Double, radius: Double)
16+
```
17+
18+
现在想象一下,你需要一个 `circumference` 方法,但是你不能修改它们的源代码。
19+
在将术语推理的概念引入编程语言之前,您唯一能做的就是在单独的类或对象中编写一个方法,如下所示:
20+
21+
```scala
22+
object CircleHelpers:
23+
def circumference(c: Circle): Double = c.radius * math.Pi * 2
24+
```
25+
26+
你可以像这样用该方法:
27+
28+
```scala
29+
val aCircle = Circle(2, 3, 5)
30+
31+
// without extension methods
32+
CircleHelpers.circumference(aCircle)
33+
```
34+
35+
但是使用扩展方法,您可以创建一个 `circumference` 方法来处理 `Circle` 实例:
36+
37+
```scala
38+
extension (c: Circle)
39+
def circumference: Double = c.radius * math.Pi * 2
40+
```
41+
42+
在这段代码中:
43+
44+
- 扩展方法 `circumference` 将添加到 `Circle` 类型里
45+
- `c: Circle` 语法允许您在扩展方法中引用变量 `c`
46+
47+
然后在您的代码中使用 `circumference`,就像它最初是在 `Circle` 类中定义的一样:
48+
49+
```scala
50+
aCircle.circumference
51+
```
52+
53+
### 导入扩展方法
54+
55+
想象一下,`circumference` 定义在`lib` 包中,你可以通过以下方式导入它
56+
57+
```scala
58+
import lib.circumference
59+
60+
aCircle.circumference
61+
```
62+
63+
如果缺少导入,编译器还会通过显示详细的编译错误消息来支持您,如下所示:
64+
65+
```text
66+
value circumference is not a member of Circle, but could be made available as an extension method.
67+
68+
The following import might fix the problem:
69+
70+
import lib.circumference
71+
```
72+
73+
## 讨论
74+
75+
`extension` 关键字声明您将要在括号中的类型上定义一个或多个扩展方法。
76+
要在一个类型上定义多个扩展方法,请使用以下语法:
77+
78+
```scala
79+
extension (c: Circle)
80+
def circumference: Double = c.radius * math.Pi * 2
81+
def diameter: Double = c.radius * 2
82+
def area: Double = math.Pi * c.radius * c.radius
83+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
---
2+
title: Given 导入
3+
type: section
4+
description: This page demonstrates how 'given' import statements work in Scala 3.
5+
num: 62
6+
previous-page: ca-context-bounds
7+
next-page: ca-extension-methods
8+
---
9+
10+
11+
为了更清楚地说明当前作用域中的 given 来自何处,我们使用一种特殊形式的 `import` 语句来导入 `given` 实例。
12+
此示例中显示了基本形式:
13+
14+
```scala
15+
object A:
16+
class TC
17+
given tc: TC = ???
18+
def f(using TC) = ???
19+
20+
object B:
21+
import A.* // import all non-given members
22+
import A.given // import the given instance
23+
```
24+
25+
在此代码中,对象 `B``import A.*` 子句导入 `A` 的所有成员*除了* `given` 实例 `tc`
26+
相反,第二个导入 `import A.given` **导入 `given` 实例。
27+
两个 `import` 子句也可以合并为一个:
28+
29+
```scala
30+
object B:
31+
import A.{given, *}
32+
```
33+
34+
## 讨论
35+
36+
通配符选择器 `*` 将除 given 或扩展之外的所有定义都导入作用域,而 `given` 选择器将所有 *given* 定义---包括由扩展而来的定义---导入作用域。
37+
38+
这些规则有两个主要优点:
39+
40+
- 更清楚当前作用域内 given 的来源。
41+
特别是,在一长串其他通配符导入中无法隐藏导入的 given 。
42+
- 它可以导入所有 given ,而无需导入任何其他内容。
43+
这很重要,因为 given 是可以匿名的,因此通常使用命名导入是不切实际的。
44+
45+
“导入 given”语法的更多示例见 [package 和 import 章节][imports]
46+
47+
48+
[imports]: {% link _overviews/scala3-book/packaging-imports.md %}

0 commit comments

Comments
 (0)