Scala 和 Groovy 之间的主要区别是什么?

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

What are the key differences between Scala and Groovy?

scalagroovy

提问by Leif

On the surface Groovy and Scala look pretty similar, aside from Scala being statically typed, and Groovy dynamic.

从表面上看,Groovy 和 Scala 看起来非常相似,除了 Scala 是静态类型的,而 Groovy 是动态类型的。

  • What are the other key differences, and advantages each have over the other?
  • How similar are they really?
  • Is there competition between the two?
    • If so, who do you think will win in the long run?
  • 其他主要区别和优势分别是什么?
  • 他们到底有多相似?
  • 两者之间有竞争吗?
    • 如果是这样,从长远来看,您认为谁会赢?

回答by James Iry

They're both object oriented languages for the JVM that have lambdas and closures and interoperate with Java. Other than that, they're extremely different.

它们都是 JVM 的面向对象语言,具有 lambda 和闭包并与 Java 互操作。除此之外,它们是非常不同的。

Groovy is a "dynamic" language in not only the sense that it is dynamically typed but that it supports dynamic meta-programming.

Groovy 是一种“动态”语言,不仅因为它是动态类型的,而且还支持动态元编程。

Scala is a "static" language in that it is statically typed and has virtually no dynamic meta-programming beyond the awkward stuff you can do in Java. Note, Scala's static type system is substantially more uniform and sophisticated than Java's.

Scala 是一种“静态”语言,因为它是静态类型的,除了在 Java 中可以做的笨拙的事情之外,几乎没有动态元编程。请注意,Scala 的静态类型系统比 Java 的更加统一和复杂。

Groovy is syntactically influenced by Java but semantically influenced more by languages like Ruby.

Groovy 在语法上受 Java 的影响,但在语义上受 Ruby 等语言的影响更大。

Scala is syntactically influenced by both Ruby and Java. It is semantically influenced more by Java, SML, Haskell, and a very obscure OO language called gBeta.

Scala 在语法上受到 Ruby 和 Java 的影响。它在语义上更多地受到 Java、SML、Haskell 和一种名为 gBeta 的非常晦涩的面向对象语言的影响。

Groovy has "accidental" multiple dispatch due to the way it handles Java overloading.

由于 Groovy 处理 Java 重载的方式,它具有“意外”多重分派。

Scala is single dispatch only, but has SML inspired pattern matching to deal with some of the same kinds of problems that multiple dispatch is meant to handle. However, where multiple dispatch can only dispatch on runtime type, Scala's pattern matching can dispatch on runtime types, values, or both. Pattern matching also includes syntactically pleasant variable binding. It's hard to overstress how pleasant this single feature alone makes programming in Scala.

Scala 只是单分派,但受 SML 启发的模式匹配可以处理一些与多分派旨在处理的相同类型的问题。然而,多个分派只能在运行时类型上分派,Scala 的模式匹配可以在运行时类型、值或两者上分派。模式匹配还包括句法上令人愉快的变量绑定。很难过分强调单独使用 Scala 进行编程是多么令人愉快。

Both Scala and Groovy support a form of multiple inheritance with mixins (though Scala calls them traits).

Scala 和 Groovy 都支持一种带有 mixin 的多重继承形式(尽管 Scala 称它们为特征)。

Scala supports both partial function application and currying at the language level, Groovy has an awkward "curry" method for doing partial function application.

Scala 在语言层面支持偏函数应用和柯里化,Groovy 有一个笨拙的“柯里化”方法来做偏函数应用。

Scala does direct tail recursion optimization. I don't believe Groovy does. That's important in functional programming but less important in imperative programming.

Scala 直接进行尾递归优化。我不相信 Groovy 会。这在函数式编程中很重要,但在命令式编程中不太重要。

Both Scala and Groovy are eagerly evaluated by default. However, Scala supports call-by-name parameters. Groovy does not - call-by-name must be emulated with closures.

默认情况下,Scala 和 Groovy 都会急切地求值。但是,Scala 支持按名称调用参数。Groovy 没有 - 必须使用闭包来模拟按名称调用。

Scala has "for comprehensions", a generalization of list comprehensions found in other languages (technically they're monad comprehensions plus a bit - somewhere between Haskell's do and C#'s LINQ).

Scala 有“for comprehensions”,这是在其他语言中发现的列表推导式的概括(从技术上讲,它们是单子推导式加上一点——介于 Haskell 的 do 和 C# 的 LINQ 之间)。

Scala has no concept of "static" fields, inner classes, methods, etc - it uses singleton objects instead. Groovy uses the static concept.

Scala 没有“静态”字段、内部类、方法等的概念——它使用单例对象代替。Groovy 使用静态概念。

Scala does not have built in selection of arithmetic operators in quite the way that Groovy does. In Scala you can name methods very flexibly.

Scala 没有像 Groovy 那样内置算术运算符的选择。在 Scala 中,您可以非常灵活地命名方法。

Groovy has the elvis operator for dealing with null. Scala programmers prefer to use Option types to using null, but it's easy to write an elvis operator in Scala if you want to.

Groovy 有处理空值的 elvis 操作符。Scala 程序员更喜欢使用 Option 类型而不是使用 null,但如果您愿意,在 Scala 中编写 elvis 运算符很容易。

Finally, there are lies, there are damn lies, and then there are benchmarks. The computer language benchmarks game ranks Scala as being between substantially faster than Groovy (ranging from twice to 93 times as fast) while retaining roughly the same source size. benchmarks.

最后,有谎言,有该死的谎言,然后是基准。计算机语言基准测试游戏将 Scala 列为比 Groovy 快得多(速度从两倍到 93 倍不等),同时保持大致相同的源代码大小。基准

I'm sure there are many, many differences that I haven't covered. But hopefully this gives you a gist.

我敢肯定还有很多很多差异我没有涉及到。但希望这能给你一个要点。

Is there a competition between them? Yes, of course, but not as much as you might think. Groovy's real competition is JRuby and Jython.

他们之间有竞争吗?是的,当然,但没有你想象的那么多。Groovy 真正的竞争对手是 JRuby 和 Jython。

Who's going to win? My crystal ball is as cracked as anybody else's.

谁会赢?我的水晶球和其他人一样破碎。

回答by hamsterofdeath

scala is meant to be an oo/functional hybrid language and is verywell planned and designed. groovy is more like a set of enhancements that many people would love to use in java. i took a closer look at both, so i can tell :)

阶意味着是一个面向对象的/功能性混合语言,并且非常良好的规划和设计。groovy 更像是许多人喜欢在 Java 中使用的一组增强功能。我仔细观察了两者,所以我可以知道:)

neither of them is better or worse than the other. groovy is very good at meta-programming, scala is very good at everything that does not need meta-programming, so...i tend to use both.

他们中的任何一个都不比另一个更好或更差。groovy 非常擅长元编程,scala 非常擅长不需要元编程的一切,所以......我倾向于同时使用两者。

回答by jasonnerothin

Scala has Actors, which make concurrency much easier to implement. And Traits which give true, typesafe multiple inheritance.

Scala 有 Actors,这使得并发更容易实现。和提供真正的、类型安全的多重继承的特征。

回答by Don Werve

You've hit the nail on the head with the static and dynamic typing. Both are part of the new generation of dynamic languages, with closures, lambda expressions, and so on. There are a handful of syntactic differences between the two as well, but functionally, I don't see a huge difference between Groovy and Scala.

您已经对静态和动态类型一目了然。两者都是新一代动态语言的一部分,具有闭包、lambda 表达式等。两者在语法上也有一些差异,但在功能上,我没有看到 Groovy 和 Scala 之间的巨大差异。

Scala implements Lists a bit differently; in Groovy, pretty much everything is an instance of java.util.List, whereas Scala uses both Lists and primitive arrays. Groovy has (I think) better string interpolation.

Scala 实现列表有点不同;在 Groovy 中,几乎所有东西都是 java.util.List 的一个实例,而 Scala 同时使用列表和原始数组。Groovy 具有(我认为)更好的字符串插值。

Scala is faster, it seems, but the Groovy folks are really pushing performance for the 2.0 release. 1.6 gave a huge leap in speed over the 1.5 series.

Scala 似乎更快,但 Groovy 的人确实在推动 2.0 版本的性能。1.6 在速度上比 1.5 系列有了巨大的飞跃。

I don't think that either language will really 'win', as they target two different classes of problems. Scala is a high-performance language that is very Java-like without having quite the same level of boilerplate as Java. Groovy is for rapid prototyping and development, where speed is less important than the time it takes for programmers to implement the code.

我认为这两种语言都不会真正“获胜”,因为它们针对两类不同的问题。Scala 是一种非常类似于 Java 的高性能语言,没有与 Java 完全相同的样板级别。Groovy 用于快速原型设计和开发,其中速度不如程序员实现代码所需的时间重要。

回答by More Than Five

Scala has a much steeper learning curve than Groovy. Scala has much more support for functional programming with its pattern matching and tail based recursion, meaning more tools for pure FP.

Scala 的学习曲线比 Groovy 陡得多。Scala 通过其模式匹配和基于尾的递归对函数式编程提供更多支持,这意味着更多用于纯 FP 的工具。

回答by Veerbhan Tahlani

Scala also has dynamica compilation and I have done it using twitter eval lib (https://github.com/twitter/util). I kept scala code in a flat file(without any extension) and using eval created scala class at run time. I would say scala is meta programming and has feature of dynamic complication

Scala 也有 dynamica 编译,我已经使用 twitter eval lib ( https://github.com/twitter/util)完成了它。我将 Scala 代码保存在一个平面文件中(没有任何扩展名),并在运行时使用 eval 创建的 Scala 类。我会说 Scala 是元编程并且具有动态复杂性的特征