Scala 编程语言的目的是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1065720/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 01:29:45  来源:igfitidea点击:

What is the purpose of Scala programming language?

scalaprogramming-languages

提问by user68109

It is my opinion that every language was created for a specific purpose. What was Scala created for and what problems does it best solve?

我认为每种语言都是为特定目的而创建的。Scala 是为什么而创建的,它最能解决什么问题?

采纳答案by oxbow_lakes

One of the things mentioned in talks by Martin Oderskyon Scala is it being a language which scales well to tackle various problems. He wasn't talking about scaling in the sense of performance but in the sense that the language itself can seemto be expanded via libraries. So that:

Martin Odersky在关于 Scala 的演讲中提到的一件事是它是一种可以很好地扩展以解决各种问题的语言。他不是在谈论性能意义上的扩展,而是在语言本身似乎可以通过库进行扩展的意义上。以便:

val lock = new ReentrantReadWriteLock
lock withReadLock {
   //do stuff
}

Looks like there is some special syntactic sugar for dealing with j.u.clocks. But this is not the case, it's just using the scala language in such a way as it appearsto be. The code is more readable, isn't it?

看起来有一些特殊的语法糖来处理j.u.c锁。但事实并非如此,它只是以看起来如此的方式使用 scala 语言。代码更具可读性,不是吗?

In particular the various parsing rules of the scala language make it very easy to create libraries which look like a domain-specific language (or DSL). Look at scala-test for example:

特别是 scala 语言的各种解析规则使得创建看起来像特定领域语言(或 DSL)的库变得非常容易。以 scala-test 为例:

describe("MyCoolClass") { 
  it("should do cool stuff") {
     val c = new MyCoolClass
     c.prop should be ("cool")
  }
} 

(There are lots more examples of this - I found out this oneyesterday). There is much talk about which new features are going in the Java language in JDK7 (project coin). Many of these features are special syntactic sugar to deal with some specific issue. Scala has been designed with some simple rules that mean new keywords for every little annoyance are not needed.

(还有很多这样的例子——我昨天发现了这个)。关于 JDK7 中 Java 语言的哪些新特性有很多讨论(项目币)。许多这些功能是特殊的语法糖来处理一些特定的问题。Scala 设计了一些简单的规则,这意味着不需要为每一个小烦恼提供新的关键字。

回答by oxbow_lakes

Another goal of Scala was to bridge the gap between functionaland object-orientedlanguages. It contains many constructs inspired (i.e. copied from!) functional languages. I'm thing of the incredibly powerful pattern-matching, the actor-based concurrency frameworkand (of course) first- and higher-order functions.

Scala 的另一个目标是弥合函数语言和面向对象语言之间的差距。它包含许多受启发(即复制自!)函数式语言的构造。我是非常强大的模式匹配基于角色的并发框架和(当然)一阶和高阶函数的东西。

Of course, your question said that there was a specific purposeand I've just given 3 separate reasons; you'll probably have to ask Martin Odersky!

当然,你的问题说有一个特定的目的,我只给出了 3 个不同的原因;你可能得问问马丁奥德斯基!

回答by Texas

Agree with previous answers but recommend the Introduction to An Overview of the Scala Programming Language:

同意以前的答案,但推荐Scala 编程语言概述简介:

The work on Scala stems from a research effort to develop better language support for component software. There are two hypotheses that we would like to validate with the Scala experiment. First, we postulate that a programming language for component software needs to be scalable in the sense that the same concepts can describe small as well as large parts. Therefore, we concentrate on mechanisms for abstraction, composition, and decomposition rather than adding a large set of primitives which might be useful for components at some level of scale, but not at other levels. Second, we postulate that scalable support for components can be provided by a programming language which unifes and generalizes object-oriented and functional programming. For statically typed languages, of which Scala is an instance, these two paradigms were up to now largely separate. (Odersky)

Scala 的工作源于为组件软件开发更好的语言支持的研究工作。我们希望通过 Scala 实验验证两个假设。首先,我们假设组件软件的编程语言需要具有可扩展性,因为相同的概念可以描述小部件和大部件。因此,我们专注于抽象、组合和分解的机制,而不是添加大量原语,这些原语可能对某些级别的组件有用,但对其他级别的组件没有用。其次,我们假设可以通过统一和概括面向对象和函数式编程的编程语言提供对组件的可扩展支持。对于静态类型语言,其中 Scala 是一个实例,到目前为止,这两种范式在很大程度上是分开的。(奥德斯基)

I'd personally classify Scala alongside Python in terms of which problems it solves and how. The conspicuous difference and occasional complaint is Type complexity. I agree Scala's abstractions are complicated and at times seemingly convoluted but for a few points:

我个人将 Scala 与 Python 一起归类为它解决哪些问题以及如何解决。显着的差异和偶尔的抱怨是类型复杂性。我同意 Scala 的抽象很复杂,有时看起来很复杂,但有几点:

  1. They're also mostly optional.
  2. Scala's compiler is like free testing and documentation as cyclomatic complexity and lines of code escalate.
  3. When aptly implemented Scala can perform otherwise all but impossible operations behind consistent and coherent APIs. From Scala 2.8 Collections:
  1. 它们也大多是可选的。
  2. 随着圈复杂度和代码行数的增加,Scala 的编译器就像免费的测试和文档。
  3. 当恰当地实现 Scala 时,它可以在一致和连贯的 API 背后执行几乎不可能的操作。来自Scala 2.8 集合

For instance, a String (or rather: its backing class RichString) can be seen as a sequence of Chars, yet it is not a generic collection type. Nevertheless, mapping a character to character map over a RichString should again yield a RichString, as in the following interaction with the Scala REPL:

例如,一个字符串(或者更确切地说:它的支持类 RichString)可以被看作是一个字符序列,但它不是一个通用的集合类型。尽管如此,在 RichString 上将字符映射到字符映射应该再次产生 RichString,如以下与 Scala REPL 的交互:

scala> "abc" map (x => (x + 1).toChar)
res1: scala.runtime.RichString = bcd

But what happens if one applies a function from Char to Int to a string? In that case, we cannot produce a string as result, it has to be some sequence of Int elements instead. Indeed one gets:

但是,如果将一个从 Char 到 Int 的函数应用于字符串会发生什么?在这种情况下,我们不能产生一个字符串作为结果,它必须是一些 Int 元素序列。确实有人得到:

"abc" map (x => (x + 1))
res2: scala.collection.immutable.Vector[Int] = Vector(98, 99, 100)

So it turns out that map yields different types depending on what the result type of the passed function argument is! (Odersky)

所以结果是 map 产生不同的类型,这取决于传递的函数参数的结果类型是什么!(奥德斯基)

回答by oxbow_lakes

One more of the original design goals was of course to create a language which runs on the Java Virtual Machine and is fully interoperable with Java classes. This has (at least) two advantages:

另一个最初的设计目标当然是创建一种在 Java 虚拟机上运行并且可以与 Java 类完全互操作的语言。这(至少)有两个优点:

  • you can take advantage of the ubiquity, stability, features and reputation of the JVM. (think management extensions, JIT compilation, advanced Garbage Collection etc)
  • you can still use all your favourite Java libraries, both 3rd party and your own. If this wasn't the case, it would be a significant obstacle to using Scala commercially in many cases (mine for example).
  • 您可以利用 JVM 的普遍性、稳定性、特性和声誉。(想想管理扩展、JIT 编译、高级垃圾收集等)
  • 您仍然可以使用所有您喜欢的 Java 库,包括 3rd 方和您自己的。如果不是这种情况,在许多情况下(例如我的),这将是商业使用 Scala 的重大障碍。

回答by Bill K

Since it's functional and uses actors (as I understand it, please comment if I've got this wrong) it makes it very easy to scale nearly anything up to any number of CPUs.

由于它具有功能性并且使用了 actor(据我所知,如果我弄错了,请发表评论),因此可以很容易地将几乎任何东西扩展到任意数量的 CPU。

That said, I see Scala as kind of a test bed for new language features. Throw in the kitchen sink and see what happens.

也就是说,我认为 Scala 是一种新语言特性的测试平台。扔进厨房水槽,看看会发生什么。

My personal opinion is that for any apps involving a team of more than 3 people you are more productive with a language with Very Simple and Restrictive Syntax just because the entire job becomes more how you interact with others as opposed to just coding to make the computer do something.

我个人的观点是,对于任何涉及 3 人以上团队的应用程序,使用具有非常简单和限制性语法的语言会更有效率,因为整个工作变得更多的是您如何与他人互动,而不仅仅是编码来制造计算机做一点事。

The more people you add, the more time you are going to spend explaining what ?: means or the difference between | and || as applied to two booleans (In Java, you'll find very few people know).

你添加的人越多,你花在解释什么上的时间就越多?:意味着或 | 之间的区别。和 || 应用于两个布尔值(在 Java 中,您会发现很少有人知道)。