Skip to content

Zh cn/overviews/scala3 book/taste summary #2693

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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion _zh-cn/overviews/scala3-book/taste-collections.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: Collections
title: 集合
type: section
description: This page provides a high-level overview of the main features of the Scala 3 programming language.
language: zh-cn
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: Contextual Abstractions
title: 上下文抽象
type: section
description: This section provides an introduction to Contextual Abstractions in Scala 3.
language: zh-cn
Expand Down
2 changes: 1 addition & 1 deletion _zh-cn/overviews/scala3-book/taste-functions.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: First-Class Functions
title: 头等函数
type: section
description: This page provides an introduction to functions in Scala 3.
language: zh-cn
Expand Down
2 changes: 1 addition & 1 deletion _zh-cn/overviews/scala3-book/taste-methods.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: Methods
title: 方法
type: section
description: This section provides an introduction to defining and using methods in Scala 3.
language: zh-cn
Expand Down
4 changes: 2 additions & 2 deletions _zh-cn/overviews/scala3-book/taste-summary.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: Summary
title: 总结
type: section
description: This page provides a summary of the previous 'Taste of Scala' sections.
language: zh-cn
Expand All @@ -26,7 +26,7 @@ permalink: "/zh-cn/scala3/book/:title.html"
- 如何将对象用于多种目的
- [上下文抽象][contextual]的介绍

我们还提到,如果您更喜欢使用基于浏览器的游乐场环境而不是 Scala REPL,您还可以使用[Scastie.scala-lang.org](https://scastie.scala-lang.org/?target=dotty) 或 [ScalaFiddle.io](https://scalafiddle.io)。
我们还提到,如果您更喜欢使用基于浏览器的游乐场环境而不是 Scala REPL,您还可以使用[Scastie](https://scastie.scala-lang.org)。

Scala还有更多功能在这次旋风之旅中没有涵盖。
有关更多详细信息,请参阅本书的其余部分和[参考文档][reference]。
Expand Down
14 changes: 13 additions & 1 deletion _zh-cn/overviews/scala3-book/taste-toplevel-definitions.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: Toplevel Definitions
title: 顶层定义
type: section
description: This page provides an introduction to top-level definitions in Scala 3
language: zh-cn
Expand All @@ -17,6 +17,9 @@ permalink: "/zh-cn/scala3/book/:title.html"
在 Scala 3 中,各种定义都可以在源代码文件的 “顶层” 编写。
例如,您可以创建一个名为 _MyCoolApp.scala_ 的文件,并将以下内容放入其中:

{% tabs toplevel_1 %}
{% tab 'Scala 3 only' for=toplevel_1 %}

```scala
import scala.collection.mutable.ArrayBuffer

Expand All @@ -43,13 +46,19 @@ type Money = BigDecimal
println("show me the code".capitalizeAllWords)
```

{% endtab %}
{% endtabs %}

如代码中展示的,无需将这些定义放在 `package`, `class` 或其他构造中。

## 替换包对象

如果你熟悉Scala 2,这种方法可以取代 _包对象_。
但是,虽然更易于使用,但它们的工作方式类似:当您将定义放在名为 _foo_ 的包中时,您可以在 _foo_ 包内的所有其他包内访问该定义,例如在此示例中的 _foo.bar_ 包中:

{% tabs toplevel_2 %}
{% tab 'Scala 3 only' for=toplevel_2 %}

```scala
package foo {
def double(i: Int) = i * 2
Expand All @@ -63,6 +72,9 @@ package foo {
}
```

{% endtab %}
{% endtabs %}

本示例中使用大括号来强调包嵌套。

这种方法的好处是,您可以将定义放在名为 _com.acme.myapp_ 的包下,然后可以在 _com.acme.myapp.model_、_com.acme.myapp.controller_ 等中引用这些定义。