Skip to content

Commit ab2f720

Browse files
benluojulienrf
authored andcommitted
add code tab in _zh-cn/overviews/scala3-book/types-introduction.md
1 parent 4d36216 commit ab2f720

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,27 @@ permalink: "/zh-cn/scala3/book/:title.html"
1717
Scala 是一种独特的语言,因为它是静态类型的,但通常_感觉_它灵活和动态。
1818
例如,由于类型推断,您可以编写这样的代码而无需显式指定变量类型:
1919

20+
{% tabs hi %}
21+
{% tab 'Scala 2 and 3' %}
2022
```scala
2123
val a = 1
2224
val b = 2.0
2325
val c = "Hi!"
2426
```
27+
{% endtab %}
28+
{% endtabs %}
2529

2630
这使代码感觉是动态类型的。
2731
并且由于新特性,例如 Scala 3 中的 [联合类型][union-types],您还可以编写如下代码,非常简洁地表达出期望哪些值作为参数,哪些值作为返回的类型:
2832

33+
{% tabs union-example %}
34+
{% tab 'Scala 3 Only' %}
2935
```scala
3036
def isTruthy(a: Boolean | Int | String): Boolean = ???
3137
def dogCatOrWhatever(): Dog | Plant | Car | Sun = ???
3238
```
39+
{% endtab %}
40+
{% endtabs %}
3341

3442
正如例子所暗示的,当使用联合类型时,这些类型不必共享一个公共层次结构,您仍然可以接受它们作为参数或从方法中返回它们。
3543

0 commit comments

Comments
 (0)