简体   繁体   English

Scala的好处是什么?

[英]What Are The Benefits Of Scala?

我是一名Java开发人员,我想知道如何在Java程序中使用Scala?

Go read Daniel Spiewak 's excellent blog series about Scala. 阅读Daniel Spiewak关于Scala 的精彩博客系列 With Scala you can keep: 使用Scala,您可以保留:

  • all your Java libraries 所有的Java库
  • all the advantages of running on a JVM (ubiquity, administrative tools, profiling, garbage collection etc) 在JVM上运行的所有优点(无处不在,管理工具,分析,垃圾收集等)

But you can write Scala code: 但是你可以编写Scala代码:

  • more concise and clear than Java (especially using more functional style, such as in the collections library) 比Java更简洁明了(特别是使用更多功能样式,例如在集合库中)
  • it has closures and functions as part of the language 它有闭包和功能作为语言的一部分
  • it has operator overloading (from the perspective of usage) 它有运算符重载(从使用的角度来看)
  • it has mixins (ie interfaces which contain implementation) 它有mixins(即包含实现的接口)

Also, take a look at this recent news item post on Scala's site: " Research: Programming Style and Productivity ". 另外,请看看Scala网站上最近的新闻帖:“ 研究:编程风格和生产力 ”。

In his paper, Gilles Dubochet , describes how he investigated two aspects of programming style using eye movement tracking. 在他的论文中, Gilles Dubochet描述了他如何使用眼动追踪调查编程风格的两个方面。 He found that it is, on average, 30% faster to comprehend algorithms that use for-comprehensions and maps, as in Scala, rather than those with the iterative while-loops of Java. 他发现使用for-comprehension和map的算法平均快30%,就像在Scala中那样,而不是那些具有Java迭代while循环的算法。

And another key quote from the news item: 新闻项目的另一个重要引用:

Alex McGuire, who writes mission critical projects in Scala for power trading companies, says of Scala "The conciseness means I can see more of a program on one screen. You can get a much better overview. When I have some mathematical model to write with Java I have to keep two models in my head, the mathematical model itself and the second a model of how to implement it in Java. With Scala one model, the mathematical one, will do. Much more productive.” 在Scala为电力贸易公司撰写关键任务项目的Alex McGuire对Scala说:“简洁意味着我可以在一个屏幕上看到更多的程序。你可以得到更好的概述。当我有一些数学模型可以用Java我必须保留两个模型,数学模型本身,第二个模型是如何用Java实现它。使用Scala一个模型,数学模型,将会更有效率。“

You an read the rest of the post and other linked items there . 你读了帖子的其余部分和那里的其他链接项目。

I can name some extremely clear points in very plain language from my limited experience: 从我有限的经验中,我可以用非常简单的语言命名一些极其明确的观点:

  1. Properties. 属性。 C++ and Java had this notion of a public getter/setter function "property" wrapped around an internal class variable which led to large amounts of boilerplate code. C ++和Java有一个公共getter / setter函数“property”的概念,它包含一个内部类变量,导致大量的样板代码。 C# formalized this as a real language feature and reduced much of the boilerplate in C# 3.0 with auto-implemented properties. C#将其形式化为一种真正的语言特性,并通过自动实现的属性减少了C#3.0中的大部分样板。 Scala classes define trivial properties simply as regular read only vals or read/write vars. Scala类将简单属性定义为常规只读val或读/写变量。 The class may later choose to replace those with get or get/set methods without affecting client code. 该类可能稍后选择使用get或get / set方法替换它们而不影响客户端代码。 For this, Scala provides the most elegant solution with the least language features and complexity. 为此,Scala提供了最优雅的解决方案,具有最少的语言功能和复杂性。

  2. Arrays use regular generics. 数组使用常规泛型。 In Java/C#, generics were bolted on as an after thought and are completely separate but have overlapping behavior with arrays. 在Java / C#中,泛型作为一种思想被拴在一起,并且是完全独立的但是与数组有重叠的行为。

  3. Scala has immutable "val" as a first-class langauge feature. Scala具有不变的“val”作为一流的语言特征。 Actually, val is similar to Java final variables: the top reference is immutable, but the contents may mutate. 实际上,val类似于Java最终变量:顶部引用是不可变的,但内容可能会发生变异。

  4. Scala lets if blocks, for-yield loops, and code in braces return a value. Scala允许块,for-yield循环和大括号中的代码返回一个值。 This is very elegant in many situations. 在许多情况下,这是非常优雅的。 A very small plus is that this eliminates the need for a separate ternary operator. 一个非常小的优点是,这消除了对单独的三元运算符的需求。

  5. Scala has singleton objects rather than C++/Java/C# class static. Scala具有单例对象而不是C ++ / Java / C#类静态。 This is a cleaner solution. 这是一个更清洁的解决方案

  6. Pattern matching. 模式匹配。 Object unpacking. 对象拆包。 Very nice in a large numbers of situations. 在很多情况下非常好。

  7. Native tuples. 原生元组。

  8. Persistent immutable collections are the default and built into the standard library. 持久不可变集合是默认集合并内置于标准库中。

I am not sure you can easily use Scala in your Java programs, as in "call a Scala class from a Java class". 我不确定您是否可以在Java程序中轻松使用Scala,例如“从Java类调用Scala类”。

You can try, following the article " Mixing Java and Scala ". 您可以按照“ 混合Java和Scala ”一文进行尝试。
Relevant extracts: 相关摘录:

The problem is that the Java and Scala compilation steps are separate: you can't compile both Java and Scala files in one go. 问题是Java和Scala编译步骤是分开的:您不能一次编译Java和Scala文件。
If none of your Java files reference any Scala classes you can first compile all your Java classes, then compile your Scala classes. 如果您的Java文件都没有引用任何Scala类,则可以先编译所有Java类,然后编译Scala类。
Or, if none of your Scala files reference any Java classes you can do it the other way around. 或者,如果您的Scala文件都没有引用任何Java类,那么您可以反过来这样做。
But if you want your Java classes to have access to your Scala classes and also have Scala classes have access to your Java classes, that's a problem. 但是,如果您希望Java类可以访问您的Scala类,并且Scala类也可以访问您的Java类,那就是一个问题。

Scala code can easily call directly into Java code, but sometimes calling Scala code from Java code is trickier, since the translation from Scala into bytecode is not quite as straightforward as for Java: Scala代码可以很容易地直接调用Java代码,但有时从Java代码调用Scala代码比较棘手,因为从Scala到字节码的转换并不像Java那么简单:
sometimes the Scala compiler adds characters to symbols or makes other changes that must be explicitly handled when calling from Java. 有时,Scala编译器会在符号中添加字符,或者在从Java调用时进行必须显式处理的其他更改。
But a Scala class can implement a Java interface, and an instance of that class can be passed to a Java method expecting an instance of the interface. 但是Scala类可以实现Java接口,并且该类的实例可以传递给期望接口实例的Java方法。
The Java class then calls the interface methods on that instance exactly as if it were a Java class instance. 然后,Java类在该实例上调用接口方法,就像它是Java类实例一样。

The opposite is possible, of course, as described in Roundup: Scala for Java Refugees , from Daniel Spiewak . 当然,正如Danup Spiewak的 Roundup:Scala for Java Refugees所述 ,情况正好相反

Scala or Java: Scala或Java:

Pros: 优点:

  • Scala supports both functional and imperative OO programming styles and it advocates that both models are not conflicting with each other but yet they are orthogonal and can complement each other. Scala支持功能性和命令式OO编程风格,并且它主张两个模型彼此不冲突,但它们是正交的并且可以相互补充。 Scala doesn't require or force the programmer to use a particular style, but usually the standard is to use functional style with immutable variables when appropriate (there are several benefits of using the functional approach such as concise and short syntax and using pure functions usually reduces the amount of non-determinism and side-effects from the code), while resorting to imperative programming when the code would look simpler or more understandable. Scala不要求或强制程序员使用特定的样式,但通常标准是在适当的时候使用带有不可变变量的函数样式(使用函数方法有一些好处,例如简洁和简短的语法以及通常使用纯函数减少代码中的非确定性和副作用的数量),同时在代码看起来更简单或更易理解时采用命令式编程。
  • Scala doesn't require ; Scala不要求; at the end of each line having it optional which leads to cleaner code 在每行的末尾,它具有可选性,这导致更清晰的代码
  • In Scala functions are first class cititzens 在Scala中,函数是一流的cititzens
  • Scala supports some advanced features which are directly built in the language such as: Currying, Closures, Higher order functions, pattern matching, Higher Kinded Types, Monads, implicit params. Scala支持一些直接用语言构建的高级功能,例如:Currying,Closures,高阶函数,模式匹配,高阶类型,Monads,隐式参数。
  • Scala can interact very well with Java and both can coexist. Scala可以与Java很好地交互,两者都可以共存。 It is possible to use java libraries directly inside Scala code invoking Java classes from scala code. 可以在Scala代码中直接使用Java库,从scala代码调用Java类。
  • Has Tuples built in the language which makes life easier in several scenarios 使用语言构建的元组使得在几种情况下生活更轻松
  • Supports operator overloading 支持运算符重载
  • has a rich Ecosystem and some popular open source projects in Apache are based on it. 拥有丰富的生态系统,Apache中一些流行的开源项目就是基于它的。
  • Async and Non-blocking code is very easy to write with Scala Futures 使用Scala Futures可以很容易地编写异步和非阻塞代码
  • Scala supports the Actor model using Akka which can be highly efficient and scalable when running distributed applications in multi-threaded and parallel business use cases (Enforce encapsulation without resorting to locks, State of actors is local and not shared, changes and data is propagated via message) Scala支持使用Akka的Actor模型,当在多线程和并行业务用例中运行分布式应用程序时,它可以高效且可扩展(强制封装而不诉诸锁定,actor的状态是本地的而不是共享的,更改和数据通过信息)
  • Code tends to be shorter if compared to Java (might not be always the case) 与Java相比,代码往往更短(可能并非总是如此)

Cons: 缺点:

  • Steep learning curve if compared to Java and other languages, requires more time in general from the learner to understand all the concepts clearly. 与Java和其他语言相比,陡峭的学习曲线需要更多时间从学习者那里清楚地理解所有概念。 Has many features 有很多功能
  • It is not as well established as Java in the market since it was invented later so Java in overall is more mature and more battle-tested. 它不像Java那样在市场上建立起来,因为它是后来发明的,所以Java总体来说更成熟,更经受战斗考验。
  • Scala opens too many doors. Scala打开了太多门。 It allows a lot of complex syntax that if used in a irresponsible way might lead to code that is hard to understand. 它允许许多复杂的语法,如果以不负责任的方式使用,可能会导致难以理解的代码。 Abusing things such as operator overloading, implicit params and other constructs can be counter-productive and might ruin code legibility. 滥用运算符重载,隐式参数和其他结构等内容可能适得其反,可能会破坏代码的易读性。
  • Java is also evolving and still getting better with newer versions (such as with JDK 9 modules) Java也在不断发展,并且对于更新版本(例如使用JDK 9模块)仍然越来越好

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM