Skip to content

Commit 2be10cd

Browse files
authored
add code tab in five files. (#2753)
1 parent fd56889 commit 2be10cd

File tree

5 files changed

+83
-3
lines changed

5 files changed

+83
-3
lines changed

_overviews/scala3-book/types-generics.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,17 @@ This is how you create and use a `Stack[Int]`:
6363

6464
{% tabs stack-usage class=tabs-scala-version %}
6565
{% tab 'Scala 2' %}
66-
```
66+
```scala
6767
val stack = new Stack[Int]
6868
stack.push(1)
6969
stack.push(2)
7070
println(stack.pop()) // prints 2
7171
println(stack.pop()) // prints 1
7272
```
7373
{% endtab %}
74+
7475
{% tab 'Scala 3' %}
75-
```
76+
```scala
7677
val stack = Stack[Int]
7778
stack.push(1)
7879
stack.push(2)

_overviews/scala3-book/types-intersection.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,23 @@ Therefore, as shown, `Resettable & Growable[String]` has member methods `reset`
4141
Intersection types can be useful to describe requirements _structurally_.
4242
That is, in our example `f`, we directly express that we are happy with any value for `x` as long as it’s a subtype of both `Resettable` and `Growable`.
4343
We **did not** have to create a _nominal_ helper trait like the following:
44+
45+
{% tabs normal-trait class=tabs-scala-version %}
46+
{% tab 'Scala 2' %}
47+
```scala
48+
trait Both[A] extends Resettable with Growable[A]
49+
def f(x: Both[String]): Unit
50+
```
51+
{% endtab %}
52+
53+
{% tab 'Scala 3' %}
4454
```scala
4555
trait Both[A] extends Resettable, Growable[A]
4656
def f(x: Both[String]): Unit
4757
```
58+
{% endtab %}
59+
{% endtabs %}
60+
4861
There is an important difference between the two alternatives of defining `f`: While both allow `f` to be called with instances of `Both`, only the former allows passing instances that are subtypes of `Resettable` and `Growable[String]`, but _not of_ `Both[String]`.
4962

5063
> Note that `&` is _commutative_: `A & B` is the same type as `B & A`.

_zh-cn/overviews/scala3-book/types-generics.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,29 @@ permalink: "/zh-cn/scala3/book/:title.html"
1818
Scala 约定是使用单个字母(如 `A`)来命名这些类型参数。
1919
然后当需要时,该类型可以在类中用于方法实例参数,或返回类型:
2020

21+
{% tabs stack class=tabs-scala-version %}
22+
{% tab 'Scala 2' %}
23+
```scala
24+
// here we declare the type parameter A
25+
// v
26+
class Stack[A] {
27+
private var elements: List[A] = Nil
28+
// ^
29+
// Here we refer to the type parameter
30+
// v
31+
def push(x: A): Unit =
32+
elements = elements.prepended(x)
33+
def peek: A = elements.head
34+
def pop(): A = {
35+
val currentTop = peek
36+
elements = elements.tail
37+
currentTop
38+
}
39+
}
40+
```
41+
{% endtab %}
42+
43+
{% tab 'Scala 3' %}
2144
```scala
2245
// here we declare the type parameter A
2346
// v
@@ -33,19 +56,35 @@ class Stack[A]:
3356
elements = elements.tail
3457
currentTop
3558
```
59+
{% endtab %}
60+
{% endtabs %}
3661

3762
`Stack` 类的这个实现采用任何类型作为参数。
3863
泛型的美妙之处在于您现在可以创建一个 `Stack[Int]``Stack[String]` 等,允许您将 `Stack` 的实现重复用于任意元素类型。
3964

4065
这是创建和使用 `Stack[Int]` 的方式:
4166

67+
{% tabs stack-usage class=tabs-scala-version %}
68+
{% tab 'Scala 2' %}
69+
```scala
70+
val stack = new Stack[Int]
71+
stack.push(1)
72+
stack.push(2)
73+
println(stack.pop()) // prints 2
74+
println(stack.pop()) // prints 1
4275
```
76+
{% endtab %}
77+
78+
{% tab 'Scala 3' %}
79+
```scala
4380
val stack = Stack[Int]
4481
stack.push(1)
4582
stack.push(2)
4683
println(stack.pop()) // prints 2
4784
println(stack.pop()) // prints 1
4885
```
86+
{% endtab %}
87+
{% endtabs %}
4988

5089
> 有关如何用泛型类型表达可变的详细信息,请参阅[型变(Variance)部分][variance]
5190

_zh-cn/overviews/scala3-book/types-inferred.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,32 @@ permalink: "/zh-cn/scala3/book/:title.html"
1616

1717
与其他静态类型编程语言一样,在 Scala 中,您可以在创建新变量时_声明_类型:
1818

19+
{% tabs xy %}
20+
{% tab 'Scala 2 and 3' %}
1921
```scala
2022
val x: Int = 1
2123
val y: Double = 1
2224
```
25+
{% endtab %}
26+
{% endtabs %}
2327

2428
在这些示例中,类型分别_明确地_声明为 `Int``Double`
2529
但是,在 Scala 中,您通常不必在定义值绑定器时声明类型:
2630

31+
{% tabs abm %}
32+
{% tab 'Scala 2 and 3' %}
2733
```scala
2834
val a = 1
2935
val b = List(1, 2, 3)
3036
val m = Map(1 -> "one", 2 -> "two")
3137
```
38+
{% endtab %}
39+
{% endtabs %}
3240

3341
当你这样做时,Scala _推断_类型,如下面的 REPL 交互所示:
3442

43+
{% tabs abm2 %}
44+
{% tab 'Scala 2 and 3' %}
3545
```scala
3646
scala> val a = 1
3747
val a: Int = 1
@@ -42,5 +52,7 @@ val b: List[Int] = List(1, 2, 3)
4252
scala> val m = Map(1 -> "one", 2 -> "two")
4353
val m: Map[Int, String] = Map(1 -> one, 2 -> two)
4454
```
55+
{% endtab %}
56+
{% endtabs %}
4557

4658
事实上,大多数变量都是这样定义的,而 Scala 自动推断类型的能力是使它_感觉_像一种动态类型语言的一个特性。

_zh-cn/overviews/scala3-book/types-intersection.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@ overview-name: "Scala 3 — Book"
1212
layout: multipage-overview
1313
permalink: "/zh-cn/scala3/book/:title.html"
1414
---
15-
15+
<span class="tag tag-inline">Scala 3 only</span>
1616

1717
用于类型,`&` 运算符创建一个所谓的_相交类型_
1818
`A & B` 类型表示同时是 `A` 类型和 `B` 类型**两者**的值。
1919
例如,以下示例使用相交类型 `Resettable & Growable[String]`
2020

21+
{% tabs intersection-reset-grow %}
22+
{% tab 'Scala 3 Only' %}
2123
```scala
2224
trait Resettable:
2325
def reset(): Unit
@@ -29,6 +31,8 @@ def f(x: Resettable & Growable[String]): Unit =
2931
x.reset()
3032
x.add("first")
3133
```
34+
{% endtab %}
35+
{% endtabs %}
3236

3337
在本例中的方法 `f` 中,参数 `x` 必须*同时*既是 `Resettable` 也是 `Growable[String]`
3438

@@ -39,10 +43,21 @@ def f(x: Resettable & Growable[String]): Unit =
3943
也就是说,在我们的示例 `f` 中,我们直接表示只要 `x``Resettable``Growable` 的子类型的任意值, 我们就感到满意。
4044
我们****需要创建一个_通用_的辅助 trait,如下所示:
4145

46+
{% tabs normal-trait class=tabs-scala-version %}
47+
{% tab 'Scala 2' %}
48+
```scala
49+
trait Both[A] extends Resettable with Growable[A]
50+
def f(x: Both[String]): Unit
51+
```
52+
{% endtab %}
53+
54+
{% tab 'Scala 3' %}
4255
```scala
4356
trait Both[A] extends Resettable, Growable[A]
4457
def f(x: Both[String]): Unit
4558
```
59+
{% endtab %}
60+
{% endtabs %}
4661

4762
定义 `f` 的两种选择之间有一个重要区别:虽然两者都允许使用 `Both` 的实例调用 `f`,但只有前者允许传递属于 `Resettable``Growable[String]` 子类型的实例,后者 `Both[String]` _不允许_
4863

0 commit comments

Comments
 (0)