Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 821446e

Browse files
committedMar 8, 2022
[zh-cn] Scala 3 Features
1 parent b297d2f commit 821446e

File tree

2 files changed

+897
-6
lines changed

2 files changed

+897
-6
lines changed
 

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ type: chapter
44
description: This page begins the overview documentation of the Scala 3 language.
55
num: 1
66
previous-page:
7-
next-page:
7+
next-page: scala-features
88

99
scala3: true
1010
partof: scala3-book
@@ -14,13 +14,13 @@ permalink: "/zh-cn/scala3/book/:title.html"
1414
---
1515

1616
欢迎阅读《Scala 3》一书。
17-
本书的目标是提供对Scala语言的非正式介绍,并以相对轻松的方式涉及所有的Scala主题
18-
若您在阅读本书时想了解有关特定功能的更多信息,可以随时参阅[_参考文档_][reference]其中更详细地涵盖了Scala语言的许多新特性
17+
本书的目标是提供对 Scala 语言的非正式介绍,并以相对轻松的方式涉及所有的 Scala 主题
18+
若您在阅读本书时想了解有关特定功能的更多信息,可以随时参阅[_参考文档_][reference]其中更详细地涵盖了 Scala 语言的许多新特性
1919

20-
在本书中,我们希望证明Scala是一种优美的、富有表现力的编程语言。它具有简洁、现代的语法,支持函数式编程(FP)和面向对象编程(OOP),并提供安全的静态类型系统。
21-
Scala的语法和特性都经过了重新思考与公开辩论,并在2020年更新,比以往任何时候都更清晰、更容易理解。
20+
在本书中,我们希望证明 Scala 是一种优美的、富有表现力的编程语言。它具有简洁、现代的语法,支持函数式编程(FP)和面向对象编程(OOP),并提供安全的静态类型系统。
21+
Scala 的语法和特性都经过了重新思考与公开辩论,并在2020年更新,比以往任何时候都更清晰、更容易理解。
2222

23-
本书首先在[品味Scala”部分][taste]对Scala的许多特性进行了一次“旋风之旅”。随后的章节会提供有关这些语言特性的更多详细信息。
23+
本书首先在[品味 Scala”部分][taste]对 Scala 的许多特性进行了一次“旋风之旅”。随后的章节会提供有关这些语言特性的更多详细信息。
2424

2525
> 我们仍在撰写本书的过程中。
2626
> 您可以[帮助我们改进][contributing]
Lines changed: 891 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,891 @@
1+
---
2+
title: Scala 3 特性
3+
type: chapter
4+
description: This page discusses the main features of the Scala 3 programming language.
5+
num: 2
6+
previous-page: introduction
7+
next-page:
8+
9+
scala3: true
10+
partof: scala3-book
11+
overview-name: "Scala 3 — Book"
12+
layout: multipage-overview
13+
permalink: "/zh-cn/scala3/book/:title.html"
14+
---
15+
16+
{% comment %}
17+
The name _Scala_ comes from the word _scalable_, and true to that name, the Scala language is used to power busy websites and analyze huge data sets.
18+
This section introduces the features that make Scala a scalable language.
19+
These features are split into three sections:
20+
{% endcomment %}
21+
22+
_Scala_ 这个名字来源于 _scalable_ 一词。正如其名,Scala 语言被用于支撑高流量网站以及分析庞大的数据集。
23+
本节介绍了使 Scala 成为一门可扩展语言的特性。
24+
这些特性分为三个部分:
25+
26+
{% comment %}
27+
28+
- High-level language features
29+
- Lower-level language features
30+
- Scala ecosystem features
31+
32+
{% endcomment %}
33+
34+
- 高级语言特性
35+
- 底层语言特性
36+
- Scala 生态系统特性
37+
38+
{% comment %}
39+
I think of this section as being like an “elevator pitch.”
40+
{% endcomment %}
41+
42+
{% comment %}
43+
44+
## High-level features
45+
46+
{% endcomment %}
47+
48+
## 高级特性
49+
50+
{% comment %}
51+
Looking at Scala from the proverbial “30,000 foot view,” you can make the following statements about it:
52+
{% endcomment %}
53+
54+
从宏观视角来看 Scala,您可以对它做出以下陈述:
55+
56+
{% comment %}
57+
58+
- It’s a high-level programming language
59+
- It has a concise, readable syntax
60+
- It’s statically-typed (but feels dynamic)
61+
- It has an expressive type system
62+
- It’s a functional programming (FP) language
63+
- It’s an object-oriented programming (OOP) language
64+
- It supports the fusion of FP and OOP
65+
- Contextual abstractions provide a clear way to implement _term inference_
66+
- It runs on the JVM (and in the browser)
67+
- It interacts seamlessly with Java code
68+
- It’s used for server-side applications (including microservices), big data applications, and can also be used in the browser with Scala.js
69+
70+
{% endcomment %}
71+
72+
- 它是一种高级编程语言
73+
- 它具有简明易读的语法
74+
- 它是静态类型的(但使人感觉是动态的)
75+
- 它有一个表达力强大的类型系统
76+
- 它是一种函数式编程(FP)语言
77+
- 它是一种面向对象的编程(OOP)语言
78+
- 它支持 FP 与 OOP 的融合
79+
- 上下文抽象提供了一种清晰的方式来实现 _表达式推断_
80+
- 它在 JVM(和浏览器)上运行
81+
- 它与 Java 代码无缝交互
82+
- 它可被用于服务器端应用(包括微服务)、大数据应用,也可以在浏览器中与 Scala.js 共同使用
83+
84+
{% comment %}
85+
The following sections take a quick look at these features.
86+
{% endcomment %}
87+
88+
以下部分将对这些特性进行简要介绍。
89+
90+
{% comment %}
91+
92+
### A high-level language
93+
94+
{% endcomment %}
95+
96+
### 一门高级语言
97+
98+
{% comment %}
99+
Scala is considered a high-level language in at least two ways.
100+
First, like Java and many other modern languages, you don’t deal with low-level concepts like pointers and memory management.
101+
{% endcomment %}
102+
103+
Scala 至少在两个方面被认为是一门高级语言。
104+
首先,像 Java 和许多其他现代语言一样,您不需要与指针和内存管理等底层概念打交道。
105+
106+
{% comment %}
107+
Second, with the use of lambdas and higher-order functions, you write your code at a very high level.
108+
As the functional programming saying goes, in Scala you write _what_ you want, not _how_ to achieve it.
109+
That is, we don’t write imperative code like this:
110+
{% endcomment %}
111+
112+
其次,通过使用 lambda 与高阶函数,您可以在非常高的层次上编写代码。
113+
正如函数式编程的说法,在 Scala 中,您编写您想要 _“什么”_,而不是 _“如何”_ 去实现它。
114+
也就是说,我们不会像这样编写命令式代码:
115+
116+
```scala
117+
def double(ints: List[Int]): List[Int] = {
118+
val buffer = new ListBuffer[Int]()
119+
for (i <- ints) {
120+
buffer += i * 2
121+
}
122+
buffer.toList
123+
}
124+
125+
val newNumbers = double(oldNumbers)
126+
```
127+
128+
{% comment %}
129+
That code instructs the compiler what to do on a step-by-step basis.
130+
Instead, we write high-level, functional code using higher-order functions and lambdas like this to compute the same result:
131+
{% endcomment %}
132+
133+
这段代码指示编译器逐步执行特定操作。
134+
相反,我们使用像这样的高阶函数与 lambda 来编写高层次的函数式代码以计算出相同的结果:
135+
136+
```scala
137+
val newNumbers = oldNumbers.map(_ * 2)
138+
```
139+
140+
{% comment %}
141+
As you can see, that code is much more concise, easier to read, and easier to maintain.
142+
{% endcomment %}
143+
144+
如您所见,该代码更简洁、更容易阅读且更易于维护。
145+
146+
{% comment %}
147+
148+
### Concise syntax
149+
150+
{% endcomment %}
151+
152+
### 简明的语法
153+
154+
{% comment %}
155+
Scala has a concise, readable syntax.
156+
For instance, variables are created concisely, and their types are clear:
157+
{% endcomment %}
158+
159+
Scala 具有简明易读的语法。例如,变量的创建十分简洁,其类型也很明确。
160+
161+
```scala
162+
val nums = List(1,2,3)
163+
val p = Person("Martin", "Odersky")
164+
```
165+
166+
{% comment %}
167+
Higher-order functions and lambdas make for concise code that’s readable:
168+
{% endcomment %}
169+
170+
高阶函数与 lambda 使代码简明易读:
171+
172+
```scala
173+
nums.map(i => i * 2) // long form
174+
nums.map(_ * 2) // short form
175+
176+
nums.filter(i => i > 1)
177+
nums.filter(_ > 1)
178+
```
179+
180+
{% comment %}
181+
Traits, classes, and methods are defined with a clean, light syntax:
182+
{% endcomment %}
183+
184+
特质(Traits)、类(Class)和方法(Method)都是用简洁、轻巧的语法定义的。
185+
186+
```scala
187+
trait Animal:
188+
def speak(): Unit
189+
190+
trait HasTail:
191+
def wagTail(): Unit
192+
193+
class Dog extends Animal, HasTail:
194+
def speak() = println("Woof")
195+
def wagTail() = println("⎞⎜⎛ ⎞⎜⎛")
196+
```
197+
198+
{% comment %}
199+
Studies have shown that the time a developer spends _reading_ code to _writing_ code is at least a 10:1 ratio, so writing code that is concise _and_ readable is important.
200+
{% endcomment %}
201+
202+
研究表明,开发人员花在 _阅读_ 代码和 _编写_ 代码上的时间比例至少为 10:1。因此,编写简洁 __ 易读的代码非常重要。
203+
204+
{% comment %}
205+
206+
### A dynamic feel
207+
208+
{% endcomment %}
209+
210+
### 动态感受
211+
212+
{% comment %}
213+
Scala is a statically-typed language, but thanks to its type inference capabilities it feels dynamic.
214+
All of these expressions look like a dynamically-typed language like Python or Ruby, but they’re all Scala:
215+
{% endcomment %}
216+
217+
Scala 是一种静态类型的语言,但由于其类型推断能力,它使人感觉是动态的。所有这些表达式看起来都像 Python 或 Ruby 这样的动态类型语言代码,但其实它们都是 Scala 代码:
218+
219+
```scala
220+
val s = "Hello"
221+
val p = Person("Al", "Pacino")
222+
val sum = ints.reduceLeft(_ + _)
223+
val y = for i <- nums yield i * 2
224+
val z = nums.filter(_ > 100)
225+
.filter(_ < 10_000)
226+
.map(_ * 2)
227+
```
228+
229+
{% comment %}
230+
As Heather Miller states, Scala is considered to be a [strong, statically-typed language](https://heather.miller.am/blog/types-in-scala.html), and you get all the benefits of static types:
231+
{% endcomment %}
232+
233+
正如 Heather Miller 所说,Scala 被认为是一种[强静态类型语言](https://heather.miller.am/blog/types-in-scala.html)。您可以获得静态类型的全部益处:
234+
235+
{% comment %}
236+
237+
- Correctness: you catch most errors at compile-time
238+
- Great IDE support
239+
- Reliable code completion
240+
- Catching errors at compile-time means catching mistakes as you type
241+
- Easy and reliable refactoring
242+
- You can refactor your code with confidence
243+
- Method type declarations tell readers what the method does, and help serve as documentation
244+
- Scalability and maintainability: types help ensure correctness across arbitrarily large applications and development teams
245+
- Strong typing in combination with excellent inference enables mechanisms like [contextual abstraction]({{ site.scala3ref }}/contextual.html) that allows you to omit boilerplate code. Often, this boilerplate code can be inferred by the compiler, based on type definitions and a given context.
246+
247+
{% endcomment %}
248+
249+
- 正确性:您可以在编译时捕获大多数错误
250+
- 强大的 IDE 支持
251+
- 可靠的代码补全
252+
- 在编译时捕获错误意味着在您打字时捕获错误
253+
- 简单而可靠的重构
254+
- 您可以自信地重构您的代码
255+
- 方法类型声明告诉读者该方法的作用,并作为文档提供帮助
256+
- 可扩展性与可维护性:类型有助于在任意大小的应用程序与开发团队中确保正确性
257+
- 强类型结合优秀的推断能力可实现[上下文抽象]({{ site.scala3ref }}/contextual.html)等机制,这允许您省略样板代码。通常,这些样板代码可由编译器根据类型定义及给定的上下文推断出来。
258+
259+
{% comment %}
260+
In that list:
261+
- 'Correctness' and 'Scalability' come from Heather Miller’s page
262+
- the IDE-related quotes in this section come from the Scala.js website:
263+
- catch most errors in the IDE
264+
- Easy and reliable refactoring
265+
- Reliable code completion
266+
{% endcomment %}
267+
268+
{% comment %}
269+
270+
### Expressive type system
271+
272+
{% endcomment %}
273+
274+
### 富有表现力的类型系统
275+
276+
{% comment %}
277+
- this text comes from the current [ScalaTour](https://docs.scala-lang.org/tour/tour-of-scala.html).
278+
- TODO: all of the URLs will have to be updated
279+
280+
- i removed these items until we can replace them:
281+
* [Compound types](/tour/compound-types.html)
282+
* [conversions](/tour/implicit-conversions.html)
283+
* [Explicitly typed self references](/tour/self-types.html)
284+
{% endcomment %}
285+
286+
{% comment %}
287+
Scala’s type system enforces, at compile-time, that abstractions are used in a safe and coherent manner.
288+
In particular, the type system supports:
289+
{% endcomment %}
290+
291+
Scala 的类型系统在编译时强制要求以安全与连贯的方式使用抽象概念。特别是,该类型系统支持:
292+
293+
{% comment %}
294+
- [Inferred types]({% link _overviews/scala3-book/types-inferred.md %})
295+
- [Generic classes]({% link _overviews/scala3-book/types-generics.md %})
296+
- [Variance annotations]({% link _overviews/scala3-book/types-variance.md %})
297+
- [Upper](/tour/upper-type-bounds.html) and [lower](/tour/lower-type-bounds.html) type bounds
298+
- [Polymorphic methods](/tour/polymorphic-methods.html)
299+
- [Intersection types]({% link _overviews/scala3-book/types-intersection.md %})
300+
- [Union types]({% link _overviews/scala3-book/types-union.md %})
301+
- [Type lambdas]({{ site.scala3ref }}/new-types/type-lambdas.html)
302+
- [`given` instances and `using` clauses]({% link _overviews/scala3-book/ca-given-using-clauses.md %})
303+
- [Extension methods]({% link _overviews/scala3-book/ca-extension-methods.md %})
304+
- [Type classes]({% link _overviews/scala3-book/ca-type-classes.md %})
305+
- [Multiversal equality]({% link _overviews/scala3-book/ca-multiversal-equality.md %})
306+
- [Opaque type aliases]({% link _overviews/scala3-book/types-opaque-types.md %})
307+
- [Open classes]({{ site.scala3ref }}/other-new-features/open-classes.html)
308+
- [Match types]({{ site.scala3ref }}/new-types/match-types.html)
309+
- [Dependent function types]({{ site.scala3ref }}/new-types/dependent-function-types.html)
310+
- [Polymorphic function types]({{ site.scala3ref }}/new-types/polymorphic-function-types.html)
311+
- [Context bounds]({{ site.scala3ref }}/contextual/context-bounds.html)
312+
- [Context functions]({{ site.scala3ref }}/contextual/context-functions.html)
313+
- [Inner classes](/tour/inner-classes.html) and [abstract type members](/tour/abstract-type-members.html) as object members
314+
{% endcomment %}
315+
316+
- [推断类型]({% link _overviews/scala3-book/types-inferred.md %})
317+
- [泛型类]({% link _overviews/scala3-book/types-generics.md %})
318+
- [型变]({% link _overviews/scala3-book/types-variance.md %})
319+
- [类型上界](/tour/upper-type-bounds.html)[类型下界](/tour/lower-type-bounds.html)
320+
- [多态方法](/tour/polymorphic-methods.html)
321+
- [交叉类型]({% link _overviews/scala3-book/types-intersection.md %})
322+
- [联合类型]({% link _overviews/scala3-book/types-union.md %})
323+
- [类型 Lambda]({{ site.scala3ref }}/new-types/type-lambdas.html)
324+
- [`given` 实例与 `using` 子句]({% link _overviews/scala3-book/ca-given-using-clauses.md %})
325+
- [扩展方法]({% link _overviews/scala3-book/ca-extension-methods.md %})
326+
- [类型类]({% link _overviews/scala3-book/ca-type-classes.md %})
327+
- [多元相等]({% link _overviews/scala3-book/ca-multiversal-equality.md %})
328+
- [不透明类型别名]({% link _overviews/scala3-book/types-opaque-types.md %})
329+
- [开放类]({{ site.scala3ref }}/other-new-features/open-classes.html)
330+
- [匹配类型]({{ site.scala3ref }}/new-types/match-types.html)
331+
- [依赖函数类型]({{ site.scala3ref }}/new-types/dependent-function-types.html)
332+
- [多态函数类型]({{ site.scala3ref }}/new-types/polymorphic-function-types.html)
333+
- [上下文边界]({{ site.scala3ref }}/contextual/context-bounds.html)
334+
- [上下文函数]({{ site.scala3ref }}/contextual/context-functions.html)
335+
- 作为对象成员的[内部类](/tour/inner-classes.html)[抽象类型](/tour/abstract-type-members.html)
336+
337+
{% comment %}
338+
In combination, these features provide a powerful basis for the safe reuse of programming abstractions and for the type-safe extension of software.
339+
{% endcomment %}
340+
341+
通过结合使用,这些特性为编程抽象的安全重用及软件的类型安全扩展提供了强大的基础。
342+
343+
{% comment %}
344+
345+
### A functional programming language
346+
347+
{% endcomment %}
348+
349+
### 一门函数式编程语言
350+
351+
{% comment %}
352+
Scala is a functional programming (FP) language, meaning:
353+
{% endcomment %}
354+
355+
Scala 是一门函数式编程(FP)语言,也就是说:
356+
357+
{% comment %}
358+
359+
- Functions are values, and can be passed around like any other value
360+
- Higher-order functions are directly supported
361+
- Lambdas are built in
362+
- Everything in Scala is an expression that returns a value
363+
- Syntactically it’s easy to use immutable variables, and their use is encouraged
364+
- It has a wealth of immutable collection classes in the standard library
365+
- Those collection classes come with dozens of functional methods: they don’t mutate the collection, but instead return an updated copy of the data
366+
367+
{% endcomment %}
368+
369+
- 函数是值,可以像任何其他值一样被传递
370+
- 直接支持高阶函数
371+
- 原生地支持 Lambda
372+
- Scala 中的一切都是会返回值的表达式
373+
- 从语法上来说,使用不可变变量很容易并且此行为被鼓励
374+
- 在标准库中有大量的不可变集合类
375+
- 这些集合类带有许多函数式方法:它们不改变集合本身,而是返回数据的更新副本
376+
377+
{% comment %}
378+
379+
### An object-oriented language
380+
381+
{% endcomment %}
382+
383+
### 一门面向对象语言
384+
385+
{% comment %}
386+
Scala is an object-oriented programming (OOP) language.
387+
Every value is an instance of a class and every “operator” is a method.
388+
{% endcomment %}
389+
390+
Scala 是一门面向对象编程(OOP)语言。
391+
每个值都是一个类的实例,每个“运算符”都是一个方法。
392+
393+
{% comment %}
394+
In Scala, all types inherit from a top-level class `Any`, whose immediate children are `AnyVal` (_value types_, such as `Int` and `Boolean`) and `AnyRef` (_reference types_, as in Java).
395+
This means that the Java distinction between primitive types and boxed types (e.g. `int` vs. `Integer`) isn’t present in Scala.
396+
Boxing and unboxing is completely transparent to the user.
397+
{% endcomment %}
398+
399+
在 Scala 中,所有类型都继承自顶层类 `Any`,其直接子类是 `AnyVal`_值类型_,例如 `Int``Boolean`)和 `AnyRef`_引用类型_,与 Java 中相同)。
400+
这意味着 Scala 中不存在 Java 中原始类型和包装类型的区别(例如 `int``Integer`)。
401+
装箱与拆箱对用户来说是完全透明的。
402+
403+
{% comment %}
404+
- AnyRef above is wrong in case of strict null checking, no? On the other hand, maybe too much information to state this here
405+
- probably not worth to mention (too advanced at this point) there is AnyKind
406+
- Add the “types hierarchy” image here?
407+
{% endcomment %}
408+
409+
{% comment %}
410+
411+
### Supports FP/OOP fusion
412+
413+
{% endcomment %}
414+
415+
### 支持 FP 与 OOP 融合
416+
417+
{% comment %}
418+
NOTE: This text in the first line comes from this slide: https://twitter.com/alexelcu/status/996408359514525696
419+
{% endcomment %}
420+
421+
{% comment %}
422+
The essence of Scala is the fusion of functional programming and object-oriented programming in a typed setting:
423+
424+
- Functions for the logic
425+
- Objects for the modularity
426+
427+
As [Martin Odersky has stated](https://jaxenter.com/current-state-scala-odersky-interview-129495.html), “Scala was designed to show that a fusion of functional and object-oriented programming is possible and practical.”
428+
{% endcomment %}
429+
430+
Scala 的本质是函数式编程和面向对象编程的融合:
431+
432+
- 函数用于代表逻辑
433+
- 对象用于模块化
434+
435+
正如 [Martin Odersky 所说](https://jaxenter.com/current-state-scala-odersky-interview-129495.html),“Scala 旨在表明函数式编程与面向对象编程的融合是切实可行的。”
436+
437+
{% comment %}
438+
439+
### Term inference, made clearer
440+
441+
{% endcomment %}
442+
443+
### 表达式推断,更加清晰
444+
445+
{% comment %}
446+
Following Haskell, Scala was the second popular language to have some form of _implicits_.
447+
In Scala 3 these concepts have been completely re-thought and more clearly implemented.
448+
449+
The core idea is _term inference_: Given a type, the compiler synthesizes a “canonical” term that has that type.
450+
In Scala, a context parameter directly leads to an inferred argument term that could also be written down explicitly.
451+
452+
Use cases for this concept include implementing [type classes]({% link _overviews/scala3-book/ca-type-classes.md %}), establishing context, dependency injection, expressing capabilities, computing new types, and proving relationships between them.
453+
454+
Scala 3 makes this process more clear than ever before.
455+
Read about contextual abstractions in the [Reference documentation]({{ site.scala3ref }}/contextual.html).
456+
{% endcomment %}
457+
458+
继 Haskell 之后,Scala 是第二种具有某种形式的 _隐式_ 的流行语言。
459+
在 Scala 3 中,这些概念经过了重新考虑并更清晰地实现。
460+
461+
其核心思想是 _表达式推断_:给定一个类型,编译器会合成一个具有该类型的“规范”表达式。
462+
在 Scala 中,一个上下文参数直接导致一个被推断出的参数项的出现。该参数项也可以被显式地写出来。
463+
464+
此概念的用例包括实现[类型类]({% link _overviews/scala3-book/ca-type-classes.md %})、建立上下文、依赖注入、表达能力、计算新类型以及证明它们之间的关系。
465+
466+
Scala 3 使此过程比以往任何时候都更加清晰。
467+
请在[参考文档]({{ site.scala3ref }}/contextual.html)中阅读关于上下文抽象的内容。
468+
469+
{% comment %}
470+
471+
### Client &amp; server
472+
473+
{% endcomment %}
474+
475+
### 客户端与服务器
476+
477+
{% comment %}
478+
Scala code runs on the Java Virtual Machine (JVM), so you get all of its benefits:
479+
480+
- Security
481+
- Performance
482+
- Memory management
483+
- Portability and platform independence
484+
- The ability to use the wealth of existing Java and JVM libraries
485+
486+
In addition to running on the JVM, Scala also runs in the browser with Scala.js (and open source third-party tools to integrate popular JavaScript libraries), and native executables can be built with Scala Native and GraalVM.
487+
{% endcomment %}
488+
489+
Scala 代码在 Java 虚拟机(JVM)上运行,因此您可以获得它的全部益处:
490+
491+
- 安全性
492+
- 性能
493+
- 内存管理
494+
- 可移植性与平台独立性
495+
- 能够使用大量的现有 Java 和 JVM 库
496+
497+
除了在 JVM 上运行外,Scala 还可以通过 Scala.js (以及开源的第三方工具以集成流行的 JavaScript 库)在浏览器中运行,并且可以使用Scala Native 与 GraalVM 构建原生可执行文件。
498+
499+
{% comment %}
500+
501+
### Seamless Java interaction
502+
503+
{% endcomment %}
504+
505+
### 与 Java 无缝交互
506+
507+
{% comment %}
508+
You can use Java classes and libraries in your Scala applications, and you can use Scala code in your Java applications.
509+
In regards to the second point, large libraries like [Akka](https://akka.io) and the [Play Framework](https://www.playframework.com) are written in Scala, and can be used in Java applications.
510+
{% endcomment %}
511+
512+
您可以在 Scala 应用程序中使用 Java 类和库,也可以在 Java 应用程序中使用 Scala 代码。
513+
对于第二点来说,诸如 [Akka](https://akka.io)[Play Framework](https://www.playframework.com) 之类的大型库是用 Scala 编写的,并且它们可以在 Java 应用程序中使用。
514+
515+
{% comment %}
516+
In regards to the first point, Java classes and libraries are used in Scala applications every day.
517+
For instance, in Scala you can read files with a Java `BufferedReader` and `FileReader`:
518+
{% endcomment %}
519+
520+
对于第一点来说,Scala 应用程序中每天都会用到 Java 类和库。
521+
例如,在 Scala 中,您可以使用 Java 的 `BufferedReader``FileReader` 来读取文件:
522+
523+
```scala
524+
import java.io.*
525+
val br = BufferedReader(FileReader(filename))
526+
// read the file with `br` ...
527+
```
528+
529+
{% comment %}
530+
Using Java code in Scala is generally seamless.
531+
532+
Java collections can also be used in Scala, and if you want to use Scala’s rich collection class methods with them, you can convert them with just a few lines of code:
533+
{% endcomment %}
534+
535+
在 Scala 中使用 Java 代码通常是无缝衔接的。
536+
537+
Java 集合也可以在 Scala 中使用, 如果您想将 Scala 丰富的集合类方法与其一起使用,只需几行代码即可转换它们:
538+
539+
540+
```scala
541+
import scala.jdk.CollectionConverters.*
542+
val scalaList: Seq[Integer] = JavaClass.getJavaList().asScala.toSeq
543+
```
544+
545+
{% comment %}
546+
547+
### Wealth of libraries
548+
549+
{% endcomment %}
550+
551+
### 丰富的库
552+
553+
{% comment %}
554+
As you’ll see in the third section of this page, Scala libraries and frameworks like these have been written to power busy websites and work with huge datasets:
555+
556+
1. The [Play Framework](https://www.playframework.com) is a lightweight, stateless, developer-friendly, web-friendly architecture for creating highly-scalable applications
557+
2. [Lagom](https://www.lagomframework.com) is a microservices framework that helps you decompose your legacy monolith and build, test, and deploy entire systems of reactive microservices
558+
3. [Apache Spark](https://spark.apache.org) is a unified analytics engine for big data processing, with built-in modules for streaming, SQL, machine learning and graph processing
559+
560+
The [Awesome Scala list](https://github.com/lauris/awesome-scala) shows dozens of additional open source tools that developers have created to build Scala applications.
561+
562+
In addition to server-side programming, [Scala.js](https://www.scala-js.org) is a strongly-typed replacement for writing JavaScript, with open source third-party libraries that include tools to integrate with Facebook’s React library, jQuery, and more.
563+
{% endcomment %}
564+
565+
正如您将在本页的第三部分中所看到的那样,已经有诸如此类的 Scala 库和框架被编写出来用于支撑高流量网站以及分析庞大的数据集:
566+
567+
1. [Play Framework](https://www.playframework.com) 是一种用于创建高度可扩展应用程序的轻量级、无状态、对开发者及Web友好的架构
568+
2. [Lagom](https://www.lagomframework.com) 是一种微服务框架,可帮助您分解遗留的单体应用并构建、测试和部署整个响应式微服务系统
569+
3. [Apache Spark](https://spark.apache.org) 是一种面向大规模数据处理的统一分析引擎,内置流、SQL、机器学习和图形处理等模块
570+
571+
[Awesome Scala 列表](https://github.com/lauris/awesome-scala)展示了开发人员为构建 Scala 应用程序而创建的许多其他开源工具。
572+
573+
除了服务器端编程之外,[Scala.js](https://www.scala-js.org) 是一款用于编写 JavaScript 应用的强类型替代方案。其开源的第三方库包含支持与 Facebook 的 React、jQuery 及其他库等集成的工具。
574+
575+
{% comment %}
576+
The Lower-Level Features section is like the second part of an elevator pitch.
577+
Assuming you told someone about the previous high-level features and then they say, “Tell me more,” this is what you might tell them.
578+
{% endcomment %}
579+
580+
{% comment %}
581+
582+
## Lower-level language features
583+
584+
{% endcomment %}
585+
586+
## 底层语言特性
587+
588+
{% comment %}
589+
Where the previous section covered high-level features of Scala 3, it’s interesting to note that at a high level you can make the same statements about both Scala 2 and Scala 3.
590+
A decade ago Scala started with a strong foundation of desirable features, and as you’ll see in this section, those benefits have been improved with Scala 3.
591+
{% endcomment %}
592+
593+
上一节介绍了 Scala 3 的高级特性,有趣的是,您可以从高层次上对 Scala 2 和 Scala 3 作出相同的表述。
594+
十年前,Scala 就为各种理想特性打下了坚实基础,正如您在本节中即将看到的那样,这些效益在 Scala 3 中得到了提高。
595+
596+
{% comment %}
597+
At a “sea level” view of the details---i.e., the language features programmers use everyday---Scala 3 has significant advantages over Scala 2:
598+
599+
- The ability to create algebraic data types (ADTs) more concisely with enums
600+
- An even more concise and readable syntax:
601+
- The “quiet” control structure syntax is easier to read
602+
- Optional braces
603+
- Fewer symbols in the code creates less visual noise, making it easier to read
604+
- The `new` keyword is generally no longer needed when creating class instances
605+
- The formality of package objects have been dropped in favor of simpler “top level” definitions
606+
- A grammar that’s more clear:
607+
- Multiple different uses of the `implicit` keyword have been removed; those uses are replaced by more obvious keywords like `given`, `using`, and `extension`, focusing on intent over mechanism (see the [Givens][givens] section for details)
608+
- [Extension methods][extension] replace implicit classes with a clearer and simpler mechanism
609+
- The addition of the `open` modifier for classes makes the developer intentionally declare that a class is open for modification, thereby limiting ad-hoc extensions to a code base
610+
- [Multiversal equality][multiversal] rules out nonsensical comparisons with `==` and `!=` (i.e., attempting to compare a `Person` to a `Planet`)
611+
- Macros are implemented much more easily
612+
- Union and intersection offer a flexible way to model types
613+
- Trait parameters replace and simplify early initializers
614+
- [Opaque type aliases][opaque_types] replace most uses of value classes, while guaranteeing the absence of boxing
615+
- Export clauses provide a simple and general way to express aggregation, which can replace the previous facade pattern of package objects inheriting from classes
616+
- The procedure syntax has been dropped, and the varargs syntax has been changed, both to make the language more consistent
617+
- The `@infix` annotation makes it obvious how you want a method to be applied
618+
- The [`@targetName`]({{ site.scala3ref }}/other-new-features/targetName.html) method annotation defines an alternate name for the method, improving Java interoperability, and letting you provide aliases for symbolic operators
619+
620+
It would take too much space to demonstrate all of those features here, but follow the links in the items above to see those features in action.
621+
All of these features are discussed in detail in the *New*, *Changed*, and *Dropped* features pages in the [Overview documentation][reference].
622+
{% endcomment %}
623+
624+
以小见大,从程序员日常使用的语言特性来看,Scala 3 比 Scala 2 具有显著优势:
625+
626+
- 可以用枚举更简洁地创建代数数据类型(ADT)
627+
- 更简明易读的语法:
628+
- “干净”的控制结构语法更容易阅读
629+
- 可选的大括号
630+
- 代码中包含更少的符号,因此会产生更少的视觉噪音,使其更容易阅读
631+
- 创建类实例时一般不再需要 `new` 关键字
632+
- 弃用了包对象,转而使用更简单的“顶层”定义
633+
- 更清晰的语法:
634+
- 移除了 `implicit` 关键字的多种不同用法,这些用法被更显而易见的关键字所取代,如 `given``using`、和 `extension`,以此将关注重点放在意图而不是机制上(详见 [Givens][givens] 部分)
635+
- [扩展方法][extension]通过更加清晰简单的机制取代了隐式类
636+
- 为类添加了 `open` 修饰符,使开发者能够有意识地声明一个类是可以被修改的,从而限制对代码库的临时扩展
637+
- [多元相等][multiversal]排除了用 `==``!=` 进行无意义的比较(即试图将 `Person``Planet` 进行比较)
638+
- 宏的实现变得更加容易
639+
- 联合与交叉提供了一种灵活的方式以建模类型
640+
- 特质参数取代并简化了早期初始化器
641+
- [不透明类型别名][opaque_types]取代了值类的大多数用途,并确保不进行装箱
642+
- 导出子句提供了一种简单而通用的方式来表现聚合,它可以取代之前继承自类的包对象的外观模式
643+
- 删除了过程语法并更改了可变参数语法,这增加了语言一致性
644+
- `@infix` 注解使得您想让一个方法被如何应用更加显而易见
645+
- [`@targetName`]({{ site.scala3ref }}/other-new-features/targetName.html) 方法注解为方法定义了一个候补名称。这提高了与 Java 的互操作性,并允许您为符号运算符提供别名
646+
647+
在这里演示所有这些特性会占用太多空间,请通过上述内容中的链接来查看这些特性的实际效果。
648+
所有这些特性都在[概述文档][reference]*新特性**变更的特性*、与*删除的特性*等页面中进行了详细讨论。
649+
650+
{% comment %}
651+
CHECKLIST OF ALL ADDED, UPDATED, AND REMOVED FEATURES
652+
=====================================================
653+
654+
New Features
655+
------------
656+
- trait parameters
657+
- super traits
658+
- creator applications
659+
- export clauses
660+
- opaque type aliases
661+
- open classes
662+
- parameter untupling
663+
- kind polymorphism
664+
- tupled function
665+
- threadUnsafe annotation
666+
- new control syntax
667+
- optional braces (experimental)
668+
- explicit nulls
669+
- safe initialization
670+
671+
CHANGED FEATURES
672+
----------------
673+
- numeric literals
674+
- structural types
675+
- operators
676+
- wildcard types
677+
- type checking
678+
- type inference
679+
- implicit resolution
680+
- implicit conversions
681+
- overload resolution
682+
- match expressions
683+
- vararg patterns
684+
- pattern bindings
685+
- pattern matching
686+
- eta expansion
687+
- compiler plugins
688+
- lazy vals initialization
689+
- main functions
690+
691+
DROPPED FEATURES
692+
----------------
693+
- DelayedInit
694+
- macros
695+
- existential types
696+
- type projection
697+
- do/while syntax
698+
- procedure syntax
699+
- package objects
700+
- early initializers
701+
- class shadowing
702+
- limit 22
703+
- XML literals
704+
- symbol literals
705+
- auto-application
706+
- weak conformance
707+
- nonlocal returns
708+
- [this] qualifier
709+
- private[this] and protected[this] access modifiers are deprecated
710+
and will be phased out
711+
{% endcomment %}
712+
713+
714+
{% comment %}
715+
716+
## Scala ecosystem
717+
718+
{% endcomment %}
719+
720+
## Scala 生态系统
721+
722+
{% comment %}
723+
TODO: I didn’t put much work into this section because I don’t know if you want
724+
to add many tools because (a) that can be seen as an endorsement and
725+
(b) it creates a section that can need more maintenance than average
726+
since tool popularity can wax and wane. One way to avoid the first
727+
point is to base the lists on Github stars and activity.
728+
{% endcomment %}
729+
730+
{% comment %}
731+
Scala has a vibrant ecosystem, with libraries and frameworks for every need.
732+
The [“Awesome Scala” list](https://github.com/lauris/awesome-scala) provides a list of hundreds of open source projects that are available to Scala developers, and the [Scaladex](https://index.scala-lang.org) provides a searchable index of Scala libraries.
733+
Some of the more notable libraries are listed below.
734+
{% endcomment %}
735+
736+
Scala 拥有一个充满活力的生态系统,有满足各种需求的库和框架。
737+
[Awesome Scala 列表](https://github.com/lauris/awesome-scala)提供了数百个可供 Scala 开发者使用的开源项目,[Scaladex](https://index.scala-lang.org) 则提供了 Scala 库的可搜索索引。
738+
以下列出了一些比较著名的库:
739+
740+
{% comment %}
741+
742+
### Web development
743+
744+
{% endcomment %}
745+
746+
### Web 开发
747+
748+
{% comment %}
749+
750+
- The [Play Framework](https://www.playframework.com) followed the Ruby on Rails model to become a lightweight, stateless, developer-friendly, web-friendly architecture for highly-scalable applications
751+
- [Scalatra](https://scalatra.org) is a tiny, high-performance, async web framework, inspired by Sinatra
752+
- [Finatra](https://twitter.github.io/finatra) is Scala services built on TwitterServer and Finagle
753+
- [Scala.js](https://www.scala-js.org) is a strongly-typed replacement for JavaScript that provides a safer way to build robust front-end web applications
754+
- [ScalaJs-React](https://github.com/japgolly/scalajs-react) lifts Facebook’s React library into Scala.js, and endeavours to make it as type-safe and Scala-friendly as possible
755+
- [Lagom](https://www.lagomframework.com) is a microservices framework that helps you decompose your legacy monolith and build, test, and deploy entire systems of Reactive microservices
756+
-
757+
{% endcomment %}
758+
759+
- [Play Framework](https://www.playframework.com) 遵循 Ruby on Rails 模型,是一种用于高度可扩展应用程序的轻量级、无状态、对开发者及Web友好的架构
760+
- [Scalatra](https://scalatra.org) 是一个小型的、高性能的、异步的网络框架,其灵感来自于 Sinatra
761+
- [Finatra](https://twitter.github.io/finatra) 是基于 TwitterServer 和 Finagle 构建的 Scala 服务
762+
- [Scala.js](https://www.scala-js.org) 是 JavaScript 的强类型替代品,它提供了一种更安全的方式以构建稳健的前端 Web 应用程序
763+
- [ScalaJs-React](https://github.com/japgolly/scalajs-react) 将 Facebook 的 React 库整合至 Scala.js,并努力使其尽可能类型安全和 Scala 友好
764+
- [Lagom](https://www.lagomframework.com) 是一种微服务框架,可帮助您分解遗留的单体应用并构建、测试和部署整个响应式微服务系统
765+
766+
{% comment %}
767+
HTTP(S) libraries:
768+
{% endcomment %}
769+
770+
HTTP(S) 库:
771+
772+
- [Akka-http](https://akka.io)
773+
- [Finch](https://github.com/finagle/finch)
774+
- [Http4s](https://github.com/http4s/http4s)
775+
- [Sttp](https://github.com/softwaremill/sttp)
776+
777+
{% comment %}
778+
JSON libraries:
779+
{% endcomment %}
780+
781+
JSON 库:
782+
783+
- [Argonaut](https://github.com/argonaut-io/argonaut)
784+
- [Circe](https://github.com/circe/circe)
785+
- [Json4s](https://github.com/json4s/json4s)
786+
- [Play-JSON](https://github.com/playframework/play-json)
787+
788+
{% comment %}
789+
Serialization:
790+
{% endcomment %}
791+
792+
序列化:
793+
794+
- [ScalaPB](https://github.com/scalapb/ScalaPB)
795+
796+
{% comment %}
797+
798+
### Science and data analysis:
799+
800+
{% endcomment %}
801+
802+
### 科学和数据分析
803+
804+
- [Algebird](https://github.com/twitter/algebird)
805+
- [Spire](https://github.com/typelevel/spire)
806+
- [Squants](https://github.com/typelevel/squants)
807+
808+
{% comment %}
809+
810+
### Big data
811+
812+
{% endcomment %}
813+
814+
### 大数据
815+
816+
- [Apache Spark](https://github.com/apache/spark)
817+
- [Apache Flink](https://github.com/apache/flink)
818+
819+
{% comment %}
820+
821+
### AI, machine learning
822+
823+
- [BigDL](https://github.com/intel-analytics/BigDL) (Distributed Deep Learning Framework for Apache Spark) for Apache Spark
824+
- [TensorFlow Scala](https://github.com/eaplatanios/tensorflow_scala)
825+
826+
{% endcomment %}
827+
828+
### 人工智能,机器学习
829+
830+
- [BigDL](https://github.com/intel-analytics/BigDL) (用于 Apache Spark 的分布式深度学习框架)
831+
- [TensorFlow Scala](https://github.com/eaplatanios/tensorflow_scala)
832+
833+
{% comment %}
834+
835+
### Functional Programming &amp; Functional Reactive Programming
836+
837+
{% endcomment %}
838+
839+
### 函数式编程 &amp; 函数式响应式编程
840+
841+
{% comment %}
842+
FP:
843+
{% endcomment %}
844+
845+
函数式编程:
846+
847+
- [Cats](https://github.com/typelevel/cats)
848+
- [Zio](https://github.com/zio/zio)
849+
850+
{% comment %}
851+
Functional reactive programming (FRP):
852+
{% endcomment %}
853+
854+
函数式响应式编程(FRP)
855+
856+
- [fs2](https://github.com/typelevel/fs2)
857+
- [monix](https://github.com/monix/monix)
858+
859+
{% comment %}
860+
861+
### Build tools
862+
863+
{% endcomment %}
864+
865+
### 构建工具
866+
867+
- [sbt](https://www.scala-sbt.org)
868+
- [Gradle](https://gradle.org)
869+
- [Mill](https://github.com/lihaoyi/mill)
870+
871+
{% comment %}
872+
873+
## Summary
874+
875+
As this page shows, Scala has many terrific programming language features at a high level, at an everyday programming level, and through its developer ecosystem.
876+
877+
{% endcomment %}
878+
879+
## 总结
880+
881+
如此页所示,Scala 在高层、日常编程层面以及贯穿开发者生态系统都具有许多出色的编程语言特性。
882+
883+
884+
[reference]: {{ site.scala3ref }}/overview.html
885+
[multiversal]: {% link _overviews/scala3-book/ca-multiversal-equality.md %}
886+
[extension]: {% link _overviews/scala3-book/ca-extension-methods.md %}
887+
[givens]: {% link _overviews/scala3-book/ca-given-using-clauses.md %}
888+
[opaque_types]: {% link _overviews/scala3-book/types-opaque-types.md %}
889+
890+
891+

0 commit comments

Comments
 (0)
Please sign in to comment.