Scala 有什么了不起?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/727078/
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:24:27  来源:igfitidea点击:

What's so great about Scala?

programming-languagesscalalanguage-features

提问by Don Werve

What makes Scala such a wonderful language, otherthan the type system? Almost everything I read about the language brings out 'strong typing' as a big reason to use Scala, but there has to be more than that. What are some of the other compelling and/or cool language features that make Scala a really useful tool?

是什么使斯卡拉这样一种美妙语言,其他比类型的系统?我读到的关于该语言的几乎所有内容都表明“强类型”是使用 Scala 的一个重要原因,但还不止于此。有哪些其他引人注目和/或很酷的语言特性使 Scala 成为真正有用的工具?

回答by andri

Here are some of the things that made me favour Scala (over, say, usual Java):

以下是让我喜欢 Scala 的一些事情(而不是通常的 Java):

a) Type inference. The Java way of doing it:

a) 类型推断。这样做的Java方式:

Map<Something, List<SomethingElse>> list = new HashMap<Something, List<SomethingElse>>()

.. is rather verbose compared to Scala. The compiler should be able to figure it out if you give one of these lists.

.. 与 Scala 相比相当冗长。如果您提供这些列表之一,编译器应该能够弄清楚。

b) First-order functions. Again, this functionality can be emulated with classes, but it's ugly.

b) 一阶函数。同样,这个功能可以用类来模拟,但它很难看。

c) Collections that have mapand fold. These two tie in with (b), and also these two are something I wish for every time I have to write Java.

c) 具有map和 的集合fold。这两个与(b)有关,而且这两个是我每次必须编写 Java 时都希望得到的。

d) Pattern matching and case classes.

d) 模式匹配和案例类。

e) Variances, which mean that if S extends T, then List[S] extends List[T]as well.

e) 方差,这意味着如果S extends T,那么List[S] extends List[T]也是。

Throw in some static types goodness as well, and I was sold on the language quite fast.

加上一些静态类型的好处,我很快就被这门语言吸引住了。

回答by Michael

It's a mash up of the best bits from a bunch of languages, what's not to love:

这是一堆语言中最好的部分的混搭,有什么不喜欢的:

  • Ruby's terse syntax
  • Java's performance
  • Erlang's Actor Support
  • Closures/Blocks
  • Convenient shorthand for maps & arrays
  • Ruby 的简洁语法
  • Java的性能
  • Erlang 的 Actor 支持
  • 关闭/块
  • 地图和数组的便捷简写

回答by Kevin Wright

Scala is often paraded for having closures and implicits. Not surprising really, as lack of closures and explicit typing are perhaps the two biggest sources of Java boilerplate!

Scala 经常因为有闭包和隐式而被夸耀。这并不奇怪,因为缺乏闭包和显式类型可能是 Java 样板的两个最大来源!

But once you examine it a little deeper, it goes far beyond Java-without-the-annoying bits, Perhaps the greatest strength of Scala is not one specific named feature, but how successful it is in unifying all of the features mentioned in other answers.

但是,一旦您更深入地研究它,它就会远远超出 Java-没有令人讨厌的部分,也许 Scala 的最大优势不是一个特定的命名功能,而是它在统一其他答案中提到的所有功能方面是多么成功.

Post Functional

后功能

The union of object orientation and functional programming for example: Because functions are objects, Scala was able to make Maps implement the Function interface, so when you use a map to look up a value, it's no different syntactically from using a function to calculate a value. In unifying these paradigms so well, Scala truly is a post-functional language.

例如面向对象和函数式编程的结合:因为函数是对象,Scala 能够让 Maps 实现 Function 接口,所以当你使用 map 查找一个值时,在语法上与使用函数计算一个值没有什么不同价值。在很好地统一这些范式方面,Scala 确实是一种后功能语言。

Or operator overloading, which is achieved by not actually having operators, they're just methods used in infix notation. So 1 + 2is just calling the +method on an integer. If the method was named plusinstead then you'd use it as 1 plus 2which is no different from 1.plus(2). This is made possible because of another combination of features; everything in Scala is an object, there are no primitives, so integers can have methods.

或者运算符重载,这是通过实际上没有运算符来实现的,它们只是中缀表示法中使用的方法。所以1 + 2只是+在整数上调用该方法。如果该方法被命名,plus那么您将使用它作为1 plus 21.plus(2). 由于另一种功能组合,这成为可能;Scala 中的一切都是对象,没有原语,所以整数可以有方法。

Other Feature Fusion

其他功能融合

Type classes were also mentioned, achieved by a combination of higher-kinded types, singleton objects, and implicits.

还提到了类型类,它是通过高级类型、单例对象和隐式的组合来实现的。

Other features that work well together are case classes and pattern matching, allowing you to easily build and deconstruct algebraic data types, without having to manually write all the tedious equality, hashcode, constructor and getter/setter logic that Java demands.

其他可以很好地协同工作的特性是 case 类和模式匹配,允许您轻松构建和解构代数数据类型,而无需手动编写 Java 要求的所有繁琐的等式、哈希码、构造函数和 getter/setter 逻辑。

Specifying immutability by default, offering lazy values, and providing first class functions all combine to give you a language that's very suited to building efficient functional data structures.

默认情况下指定不变性、提供惰性值和提供一流的函数,所有这些结合起来为您提供一种非常适合构建高效函数式数据结构的语言。

The list goes on, but I've been using Scala for over 3 years now, and I'm still amazed almost daily at how well everything just works together.

名单还在继续,但我已经使用 Scala 超过 3 年了,而且几乎每天我仍然惊讶于一切都可以很好地协同工作。

Efficient and Versatile

高效、多功能

Scala is also a small language, with a spec that (surprisingly!) only needs to be around 1/3 the size of Java's. This is partly because Java has a lot of special cases in the spec that Scala simplifies away, partly because of removing features such as primitives and operators, and partly because a lot of functionality has been moved from the language and into the libraries.

Scala 也是一种小型语言,其规范(令人惊讶!)仅需 Java 大小的 1/3 左右。这部分是因为 Java 在规范中有很多被 Scala 简化的特殊情况,部分是因为删除了诸如原语和运算符之类的特性,部分是因为许多功能已经从语言移到了库中。

As a benefit of this, all the techniques available to the Scala library authors are also available to any Scala user, which makes it a great language for defining your own control-flow constructs and for building DSLs. This has been used to great effect in projects like Akka - a 3rd-party Actor framework.

这样做的一个好处是,Scala 库作者可用的所有技术也可供任何 Scala 用户使用,这使其成为定义您自己的控制流结构和构建 DSL 的绝佳语言。这已经在像 Akka 这样的项目中使用了很好的效果 - 一个 3rd 方 Actor 框架。

Deep

深的

Finally, it scales the full range of programming styles.

最后,它扩展了所有编程风格。

The runtime interpreter (known as the REPL) allows you to very quickly explore ideas in an interactive session, and Scala files can also be run as scripts without needing explicit compilation. When coupled with type inference, this gives Scala the feel of a dynamic language such as Ruby, Perl, or a bash script.

运行时解释器(称为 REPL)允许您在交互式会话中非常快速地探索想法,并且 Scala 文件也可以作为脚本运行而无需显式编译。当与类型推断结合使用时,这使 Scala 感觉像是一种动态语言,例如 Ruby、Perl 或 bash 脚本。

At the other end of the spectrum, traits, classes, objects and self-types allow you to build a full-scale enterprise system based on distinct components and using dependency injection without the need of 3rd-party tools. Scala also integrates with Java libraries at a level almost on-par with native Java, and by running on the JVM can take advantage of all the speed benefits offered on that platform, as well as being perfectly usable in containers such as tomcat, or with OSGi.

另一方面,特征、类、对象和自类型允许您基于不同的组件并使用依赖注入构建一个完整的企业系统,而无需第三方工具。Scala 还与 Java 库的集成水平几乎与原生 Java 相当,并且通过在 JVM 上运行,可以利用该平台提供的所有速度优势,并且可以完美地用于容器(例如 tomcat,或操作系统。

回答by Kevin Peterson

I'm new to Scala, but my impression is:

我是 Scala 的新手,但我的印象是:

Really good JVM integration will be the driving factor. JRuby can call java and java can call JRuby code, but it's explicitly calling into another language, not the clean integration of Scala-Java. So you can use Java libraries, and even mix and match in the same project.

真正好的 JVM 集成将是驱动因素。JRuby 可以调用 java,java 也可以调用 JRuby 代码,但它是显式调用另一种语言,而不是 Scala-Java 的干净集成。因此您可以使用 Java 库,甚至可以在同一个项目中混合搭配。

I started looking at scala when I had a realization that the thing which will drive the next great language is easy concurrency. The JVM has good concurrency from a performance standpoint. I'm sure someone will say that Erlang is better, but Scala is actually usable by normal programmers.

当我意识到将推动下一个伟大语言的发展是简单的并发时,我开始研究 Scala。从性能的角度来看,JVM 具有良好的并发性。我敢肯定有人会说 Erlang 更好,但 Scala 实际上可供普通程序员使用。

Where Java falls down is that it's just so painfully verbose. It takes way too many characters to create and pass a Functor. Scala allows passing functions as arguments.

Java 失败的地方在于它太冗长了。创建和传递一个 Functor 需要太多的字符。Scala 允许将函数作为参数传递。

It isn't possible in Java to create a union type, or to apply an interface to an existing class. These are both easy in Scala.

在 Java 中无法创建联合类型,或将接口应用于现有类。这些在 Scala 中都很容易。

Static typing usually has a big penalty of verboseness. Scala eliminates this downside while still giving the upside of static typing, which is compile time type checking, and it makes code assist in editors easier.

静态类型通常会带来很大的冗长惩罚。Scala 消除了这个缺点,同时仍然提供静态类型的优点,即编译时类型检查,它使编辑器中的代码辅助更容易。

The ability to extend the language. This has been the thing that has kept Lisp going for decades, and that allowed Ruby on Rails.

扩展语言的能力。这就是让 Lisp 持续数十年的东西,也让 Ruby on Rails 成为可能。

回答by Apocalisp

The type system really is Scala's most distinguishing feature. It also has a lot of syntactic conveniences over, say, Java.

类型系统确实是 Scala 最显着的特征。它也有很多语法上的便利,比如 Java。

But for me, the most compelling features of Scala are:

但对我来说,Scala 最引人注目的特性是:

In effect, these features let you approximate (and in some ways surpass) Haskell's type classes. Combined, they let you write exceptionally modular code.

实际上,这些特性让你接近(并在某些方面超越)Haskell 的类型类。结合起来,它们可以让您编写异常模块化的代码。

回答by thSoft

Just shortly:

就在不久:

  • You get the power and platform-independency of the Java libraries, but without the boilerplate and verbosity.
  • You get the simplicity and productivity of Ruby, but with static typing and compiled bytecode.
  • You get the functional goodnesses and concurrency support of Haskell, but without complete paradigm shift and with the benefits of object-orientation.
  • 您可以获得 Java 库的强大功能和平台独立性,但没有样板和冗长。
  • 您可以获得 Ruby 的简单性和生产力,但使用静态类型和编译的字节码。
  • 您可以获得 Haskell 的功能优势和并发支持,但没有完全的范式转换和面向对象的好处。

What I find especially attractive in all of its magnificient features, among others:

我发现它的所有壮丽功能都特别吸引人,其中包括:

  • Most of the object-oriented design patterns which require loads of boilerplate code in Java are supported natively, e.g. Singleton (via objects), Adapter, Decorator (via traits and implicits), Visitor (via pattern matching), Strategy (via closures) etc.
  • You can define your domain models and DSLs very concisely, then you can extend them with the necessary features (notification, association handling; parsing, serialization), without the need of code generation or frameworks.
  • And finally, there is full interoperability with the well-supported Java platform. You can mix Java and Scala in both directions. There is not much penalty nor compatibility problems when switching to Scala after having experienced the annoyances of Java which make code hard to maintain.
  • 大多数需要在 Java 中加载样板代码的面向对象设计模式都得到了本地支持,例如单例(通过对象)、适配器、装饰器(通过特征和隐式)、访问者(通过模式匹配)、策略(通过闭包)等.
  • 您可以非常简洁地定义域模型和 DSL,然后可以使用必要的功能(通知、关联处理;解析、序列化)扩展它们,而无需代码生成或框架。
  • 最后,与支持良好的 Java 平台具有完全的互操作性。您可以在两个方向上混合使用 Java 和 Scala。体验过Java 代码难维护的烦恼后,切换到Scala 并没有太大的惩罚和兼容性问题。

回答by oxbow_lakes

Expressiveness of control flow. For example, it's very common to have a collection of data which you need to process in some way. This might be a list of trades in which the processing involves grouping by some properties (the currencies of the investment instruments) and then doing a summation (to get totals-per-currency perhaps).

控制流的表现力。例如,拥有需要以某种方式处理的数据集合是很常见的。这可能是一个交易列表,其中的处理涉及按某些属性(投资工具的货币)进行分组,然后进行求和(也许是为了获得每种货币的总数)。

In Java this involves separating out a piece of code to do the grouping (a few lines of for-loop) and then another piece of code to do the summation (another for loop). In Scala, this type of thing is typically achievable in one line of code using functional programming and then folding, which reads very expressively l-to-r.

在 Java 中,这涉及分离出一段代码来进行分组(几行 for 循环),然后另一段代码进行求和(另一个 for 循环)。在 Scala 中,这种类型的事情通常可以在一行代码中使用函数式编程然后折叠来实现,从 l 到 r 读起来非常有表现力。

Of course, this is just an argument for a functional language over Java.

当然,这只是函数式语言优于 Java 的争论。

回答by Chase Seibert

Supposedly it's very easy to make Scala code run concurrentlyon multiple processors.

据说让 Scala 代码在多个处理器上并发运行非常容易。

回答by John Nilsson

The great features of Scala has already been mentioned. One thing that shines through past all features though, is how tastefully everything is integrated.

已经提到了 Scala 的强大功能。然而,在所有功能中闪耀的一件事是,所有内容的集成都是多么高雅。

Scala manages to be one of the most powerful language around without having a feeling of having bolted on features in haste. Neither are the language an academic exercise in proving a point. Innovation and really advanced concepts are brought in to the language with uncanny practicality and elegance.

Scala 设法成为最强大的语言之一,而没有匆忙地使用功能的感觉。语言也不是证明观点的学术练习。创新和真正先进的概念以不可思议的实用性和优雅被引入到语言中。

In short: Martin Odersky is a pure design genius. That is what's so great about Scala!

简而言之:Martin Odersky 是一个纯粹的设计天才。这就是 Scala 的伟大之处!