为什么 Scala 非常复杂?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2573899/
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
Why is Scala very complex?
提问by Anantha Kumaran
I am a student. I learned java during the 2nd year. Now i am in fourth year. I got bored with java and i started to learn Scala. As i learn it, i find it being very complex (although i love it). My question may apply to all new complex language.
我是学生。我在第二年学习了java。现在我大四了。我对 Java 感到厌烦,开始学习 Scala。当我学习它时,我发现它非常复杂(尽管我喜欢它)。我的问题可能适用于所有新的复杂语言。
Why scala is complex?
is it because we need to create complex softwares?
or i am the only one who thinks it is complex?
为什么 Scala 很复杂?
是因为我们需要创建复杂的软件吗?
或者我是唯一一个认为它很复杂的人?
回答by codesurgeon
@Anantha For the last ten years most universities have been teaching their students Java as first language. I've heard of a strikingly high number of cases where it even remains the only language students get to learn while at college - unless they pick up something else on their own, that is.
@Anantha 在过去的十年中,大多数大学都将 Java 作为第一语言教给学生。我听说过非常多的案例,它甚至仍然是学生在大学期间学习的唯一语言——除非他们自己学习其他东西。
Purely from a language viewpoint, Java's three most characterizing features are
纯粹从语言的角度来看,Java 的三个最具特色的特性是
- it's imperative
- it's object oriented
- it's garbage collected
- 势在必行
- 它是面向对象的
- 这是垃圾收集
Features 1 & 2 make it very similar to a wide array of languages from the Algol/C and C++ family. All of these languages either share similarities in their paradigm or even utilize exactly the same.
特性 1 和特性 2 使其与来自 Algol/C 和 C++ 家族的大量语言非常相似。所有这些语言要么在范式上有相似之处,要么甚至完全相同。
C# for instance, despite it's syntactic differences, Windows as main target OS and .NET framework as "class library", is very easy to pick up for a Java programmer. That is due to both languages sharing the same programming paradigm.
以 C# 为例,尽管它在语法上存在差异,但作为主要目标操作系统的 Windows 和作为“类库”的 .NET 框架对于 Java 程序员来说很容易上手。这是因为两种语言共享相同的编程范式。
Scala on the other hand - despite running on the JVM, providing easy interoperability with Java APIs - is what is commonly referred to as a multi-paradigm language. The language provides deep syntactic integration of functional programming language features, yet structures code in an object oriented fashion.
另一方面,Scala - 尽管在 JVM 上运行,但提供了与 Java API 的轻松互操作性 - 通常被称为多范式语言。该语言提供了函数式编程语言特性的深度句法集成,但以面向对象的方式构造代码。
The concept of functional programming - especially once you get into code beyond trivial tutorials - proves to be hard for devs who are only experienced with imperative, OO languages. My personal experience with helping fellow developers get up to speed on Scala et al is that it greatly helps to teach them Scheme first ;) It's a nice, small, clean Lisp dialect. It helps with conveying advanced functional concepts. If you decide to give it a shot, I recommend you have a look at "The Little Schemer" and "The Seasoned Schemer". Once you're through with both books, I bet you'll have an easier time looking right through Scala's syntax and seeing it's concepts more clearly.
函数式编程的概念——尤其是当你进入代码而不是琐碎的教程时——被证明对于只熟悉命令式面向对象语言的开发人员来说很难。我在帮助其他开发人员快速了解 Scala 等方面的个人经验是,首先教他们 Scheme 非常有帮助;) 这是一种漂亮、小巧、干净的 Lisp 方言。它有助于传达先进的功能概念。如果你决定试一试,我建议你看看“小阴谋家”和“经验丰富的阴谋家”。一旦你读完了这两本书,我敢打赌你会更轻松地了解 Scala 的语法并更清楚地了解它的概念。
In a nutshell: IMHO it's not Scala that is hardto learn, the functional programming paradigmis the reason for most devs, who've only been exposed to imperative languages, having a hard time getting up to speed.
简而言之:恕我直言,Scala 并不难学,函数式编程范式是大多数只接触过命令式语言的开发人员难以跟上速度的原因。
回答by Itay Maman
There are two questions here:
这里有两个问题:
- Is it more difficult to learn Scala than Java?
- Is code written in Scala tends to be more complicated than code written in Java?
- 学习Scala比Java难吗?
- 用 Scala 编写的代码是否比用 Java 编写的代码更复杂?
The first question is easier to answer: The Scala language is richer than Java. In particular, its type system is more expressive than Java which means that one can express more logical errors as compile time errors. However, in order to exploit these capabilities one needs to be acquainted with the different constructs of the language (dependent types, case classes, variance annotations, views, to name a few). Mastering these takes time and that's why Scala is more complicated to learn than Java.
第一个问题更容易回答:Scala 语言比 Java 丰富。特别是,它的类型系统比 Java 更具表现力,这意味着可以将更多的逻辑错误表示为编译时错误。然而,为了利用这些功能,人们需要熟悉语言的不同结构(依赖类型、案例类、差异注释、视图,仅举几例)。掌握这些需要时间,这就是为什么 Scala 比 Java 更难学习的原因。
The second question is trickier. Scala advocates claim that these new constructs make it easier to write correct programs and that the resulting code is simpler. Others are saying that Scala's additional power does not outweigh the complexity of understanding the sematnics of its constructs (For example, take a look at this talk. Search for "Scala"). This is a manifestation of broader dispute: that of statically vs. dynamically typed languages.
第二个问题比较棘手。Scala 的拥护者声称这些新结构使编写正确的程序变得更容易,并且生成的代码更简单。其他人说 Scala 的额外功能并没有超过理解其构造语义的复杂性(例如,看看这个演讲。搜索“Scala”)。这是更广泛争议的表现:静态与动态类型语言的争论。
回答by Edwin Buck
Scala is complex because it gives you flexibility. Time after time, not having enough flexibility makes it difficult to do some tasks, yet too much flexibility is like too much money, it empowers you to make the really big mistakes.
Scala 很复杂,因为它为您提供了灵活性。一次又一次,没有足够的灵活性使某些任务难以完成,但太多的灵活性就像太多的钱,它使您能够犯下真正的大错误。
Scala is both Object Oriented and Functional. Both of these language types were once considered quite complex apart (although object oriented is now more mainstream) but putting them together opens all sorts of new doors. Some of those doors look like short cuts to "mission accomplished"! Some of those doors have lions behind them. Functional programming gives you all the rope to get the job done, hang yourself, and tie your neighbourhood up in knots for years. It's up to you to not injure yourself in a functional programming language.
Scala 既是面向对象的又是函数式的。这两种语言类型曾经被认为是相当复杂的(尽管面向对象现在更加主流),但将它们放在一起打开了各种新的大门。其中一些门看起来像是“完成任务”的捷径!其中一些门后面有狮子。函数式编程为您提供了完成工作的所有绳索,将自己吊死,并将您的社区联系在一起多年。在函数式编程语言中不要伤害自己取决于您。
The key to successful Scala is to recognize that you should be a successful object oriented programmer, a successful functional programmer, and then learn how to mix the two together in ways that get you to your goal. That's a lot of work. Perhaps in the future, people will know what is the "best" approach to learning Scala, but for now, the only approach known is to be good in two different approaches to progamming, AND be good in mixing them together.
Scala 成功的关键是认识到您应该成为一名成功的面向对象程序员,一名成功的函数式程序员,然后学习如何以实现目标的方式将两者混合在一起。这是很多工作。或许在未来,人们会知道什么是学习 Scala 的“最佳”方法,但就目前而言,唯一已知的方法是擅长两种不同的编程方法,并擅长将它们混合在一起。
回答by Jesse
If you think the language is complex then you might consider learning it in stages.
如果您认为这门语言很复杂,那么您可以考虑分阶段学习。
This guide to breaking up Scala into different parts is what many people do instinctively and usually necessarily, but this guide is written by Martin (Scala's creator) so it has his unique perspective. http://www.scala-lang.org/node/8610
这份将 Scala 分解成不同部分的指南是许多人本能地做的,而且通常是必要的,但本指南是由 Martin(Scala 的创造者)编写的,所以它有他独特的视角。 http://www.scala-lang.org/node/8610
Notice that most of the constructs that freak people out are in the Library designer levels. The intermediate application developer can get a lot done without too many CS grad-school concepts.
请注意,大多数让人们感到害怕的构造都在库设计器级别。中级应用程序开发人员可以在没有太多 CS 研究生概念的情况下完成很多工作。
**
Level A1: Beginning application programmer
Java-like statements and expressions: standard operators,
method calls, conditionals, loops, try/catch
class, object, def, val, var, import, package
Infix notation for method calls
Simple closures
Collections with map, filter, etc
for-expressions
Level A2: Intermediate application programmer
Pattern matching
Trait composition
Recursion, in particular tail recursion
XML literals
Level A3: Expert application programmer
Folds, i.e. methods such as foldLeft, foldRight
Streams and other lazy data structures
Actors
Combinator parsers
Level L1: Junior library designer
Type parameters
Traits
Lazy vals
Control abstraction, currying
By-name parameters
Level L2: Senior library designer
Variance annotations
Existential types (e.g., to interface with Java wildcards)
Self type annotations and the cake pattern for dependency injection
Structural types (aka static duck typing)
Defining map/flatmap/withFilter for new kinds of for-expressions
Extractors
Level L3: Expert library designer
Early initializers
Abstract types
Implicit definitions
Higher-kinded types
**
回答by Daniel C. Sobral
Am I the only one who doesn'tthink it is complex? You can write complex stuff with it, sure. But the alternative would be notbeing able to write complex stuff, which isn't exactly an improvement. But I find the language itself very simple.
我是唯一一个谁不认为它是复杂的?你可以用它写复杂的东西,当然。但是另一种选择是无法编写复杂的东西,这并不完全是一种改进。但我发现语言本身非常简单。
What I do think you are going through is the shock of learning a secondlanguage. You'd probably find C with its pointer arithmetics very complex.
我确实认为你正在经历的是学习第二语言的冲击。您可能会发现 C 的指针运算非常复杂。
回答by Synesso
What is complex about Scala is the type system. It is very flexible, but unfortunately exposes this flexibility in complex type signatures.
Scala 的复杂之处在于类型系统。它非常灵活,但不幸的是在复杂类型签名中暴露了这种灵活性。
Or it may be that you are finding the paradigm shift to using higher order functions complex.
或者,您可能正在发现向使用复杂的高阶函数的范式转变。
Either way, I recommend you stick with it. It offers a degree of power over Java-the-language which cannot be passed up.
不管怎样,我建议你坚持下去。它提供了一定程度的 Java 语言能力,这是不可忽视的。
回答by Don Mackenzie
I'd suggest that you don't look at Scala as complex, just advanced. Scala represents a broad and surprisingly coherent advance on just about every aspect of conventional imperative programming languages, however each of these advances is easily absorbed and applied. Trying to adopt too many of Scala's improvements at once will probably result in confusion and damaged confidence.
我建议您不要将 Scala 视为复杂,而只是将其视为高级。Scala 在传统命令式编程语言的几乎每个方面都代表了广泛且令人惊讶的连贯进步,但是这些进步中的每一个都很容易被吸收和应用。试图一次采用太多 Scala 的改进可能会导致混乱和信心受损。
Scala deserves mainstream adoption but seems to suffer from it's own intoxicating effects. Trying to be content with applying small improvements is very difficult in such a rich environment but don't be put off.
Scala 值得主流采用,但似乎受到其自身令人陶醉的影响。在如此丰富的环境中,试图满足于应用小改进是非常困难的,但不要被推迟。
回答by IttayD
Scala is complex for several reasons:
Scala 之所以复杂,有以下几个原因:
It brings functional programming paradigms. The Scala Collection library is much reacher than Java's because of these.
它带来了函数式编程范式。由于这些,Scala Collection 库比 Java 库更广泛。
It allows creating modular and fluent APIs. For example, in Scala, the method #map is implemented in TraversableLike, (one of the root classes in the collection library) yet, its result type is the type of the most appropriate concrete collection (for BitSet, it will return a BitSet if the mapping converts an Int to an Int and a Set otherwise). This is made possible by the language, not compiler trickery.
它允许创建模块化和流畅的 API。例如,在 Scala 中,#map 方法在 TraversableLike(集合库中的根类之一)中实现,但其结果类型是最合适的具体集合的类型(对于 BitSet,如果映射将 Int 转换为 Int 和 Set 否则)。这是由语言实现的,而不是编译器技巧。
It is statically typed. It is easier to create a dynamic language that has the above features, but static typing gives you IDE support and performance.
它是静态类型的。创建具有上述功能的动态语言更容易,但静态类型为您提供 IDE 支持和性能。
IMHO all these features are very important and worth the additional complexity.
恕我直言,所有这些功能都非常重要,值得额外的复杂性。
I was once at the point where you are. When I encountered Scala in 2006 I abandoned it after a while of trying to learn it, for its complexity. I had to learn it for a project I'm doing and am very happy that I did so. I consider not learning it in 2006 one of the biggest mistakes in my professional life.
我曾经在你所在的地方。当我在 2006 年遇到 Scala 时,我在尝试学习它一段时间后放弃了它,因为它很复杂。我必须为我正在做的一个项目学习它,我很高兴我这样做了。我认为 2006 年没有学习它是我职业生涯中最大的错误之一。
回答by Oded
It is as complex as Java (or any modern programming language) - I can only assume you have not seen large programs in Java.
它和 Java(或任何现代编程语言)一样复杂 - 我只能假设您没有见过 Java 中的大型程序。
It is not just Object Oriented but also Functional, which is a different programming paradigm.
它不仅是面向对象的,而且是功能性的,这是一种不同的编程范式。
Don't confuse your difficulty with Functional Programming with the language being hard. Functional Programming can be very confusing to those used to Object Oriented (or Procedural) programming.
不要将函数式编程的困难与语言的困难混淆。对于那些习惯于面向对象(或过程)编程的人来说,函数式编程可能会非常混乱。
回答by Daniel Earwicker
Scala provides the power to make libraries that can be used in a very terse way. Java libraries often have a verbose, cumbersome interface. This sounds like nothing but a plus for Scala, but not necessarily. In Scala, two functions can work completely different and yet appear very similar in usage - syntax is no guide to behaviour. Whereas in Java certain things are always made obvious by the syntax.
Scala 提供了制作可以以非常简洁的方式使用的库的能力。Java 库通常具有冗长、繁琐的界面。这听起来对 Scala 来说只是一个加分项,但不一定。在 Scala 中,两个函数可以完全不同,但在用法上却非常相似——语法不是行为指南。而在 Java 中,某些事情总是通过语法变得显而易见。
Suppose you have a function fooin Java and it is called like this:
假设您有一个fooJava函数,它的调用方式如下:
foo(v > 5);
So we are apparently passing it a booleanvalue. Not so fast! Suppose Java had a feature that allowed the function footo capture the expression and evaluate it later (perhaps on a different thread). So instead of accepting a booleanvalue, fooaccepts a function that takes no parameters and returns a boolean. You can't tell what's happening by just looking at the call site; you have to know the details of how fooworks in order to know when the v > 5expression will be evaluated. Whereas in Java today, the expression will always be evaluated before foois executed.
所以我们显然是在传递一个boolean值。没那么快!假设 Java 有一个特性,允许函数foo捕获表达式并稍后对其进行评估(可能在不同的线程上)。因此,不是接受一个boolean值,而是接受一个foo不带参数并返回一个boolean. 仅查看呼叫站点,您无法知道发生了什么;您必须了解foo工作原理的详细信息,才能知道何时计算v > 5表达式。而在今天的 Java 中,表达式将始终在foo执行之前进行评估。
To Java programmers this would probably seem quite unnerving. But Scala has this exact feature.
对于 Java 程序员来说,这可能看起来很令人不安。但是 Scala 有这个确切的特性。
This breaking of the link between syntax and behaviour is something that makes Scala more liable to confuse the unwary. On the other hand, it allows embedded domain specific languages to be created, where the whole point is that there is a terse, efficient notation suited to a specific problem domain.
破坏语法和行为之间的联系使 Scala 更容易混淆粗心。另一方面,它允许创建嵌入式领域特定语言,重点是有一个适合特定问题领域的简洁、高效的符号。

