File tree Expand file tree Collapse file tree 1 file changed +8
-0
lines changed
_zh-cn/overviews/scala3-book Expand file tree Collapse file tree 1 file changed +8
-0
lines changed Original file line number Diff line number Diff line change @@ -17,19 +17,27 @@ permalink: "/zh-cn/scala3/book/:title.html"
17
17
Scala 是一种独特的语言,因为它是静态类型的,但通常_感觉_它灵活和动态。
18
18
例如,由于类型推断,您可以编写这样的代码而无需显式指定变量类型:
19
19
20
+ {% tabs hi %}
21
+ {% tab 'Scala 2 and 3' %}
20
22
``` scala
21
23
val a = 1
22
24
val b = 2.0
23
25
val c = " Hi!"
24
26
```
27
+ {% endtab %}
28
+ {% endtabs %}
25
29
26
30
这使代码感觉是动态类型的。
27
31
并且由于新特性,例如 Scala 3 中的 [ 联合类型] [ union-types ] ,您还可以编写如下代码,非常简洁地表达出期望哪些值作为参数,哪些值作为返回的类型:
28
32
33
+ {% tabs union-example %}
34
+ {% tab 'Scala 3 Only' %}
29
35
``` scala
30
36
def isTruthy (a : Boolean | Int | String ): Boolean = ???
31
37
def dogCatOrWhatever (): Dog | Plant | Car | Sun = ???
32
38
```
39
+ {% endtab %}
40
+ {% endtabs %}
33
41
34
42
正如例子所暗示的,当使用联合类型时,这些类型不必共享一个公共层次结构,您仍然可以接受它们作为参数或从方法中返回它们。
35
43
You can’t perform that action at this time.
0 commit comments