Scala特性和Java 8接口之间有什么区别和相似之处?

时间:2022-09-25 12:28:21

I am new to Scala started learning the language for fun and I am still trying to get my head around it. My understanding of Scala traits is that they are like java interfaces except that some methods can have an implementation.

我是刚接触Scala的人,我开始学习这门语言是为了好玩,而且我还在努力思考这个问题。我对Scala特性的理解是,它们就像java接口,只不过有些方法可以实现。

Java 8 is adding interfaces that can have default methods where an implementation can be provided.

Java 8添加了可以提供实现的默认方法的接口。

What are the similarities and differences between Java 8 interfaces and Scala traits?

Java 8接口和Scala特性之间有什么相似之处和不同之处?

2 个解决方案

#1


33  

Motivations for Java 8' default methods and Scala traits differ.

Java 8的默认方法和Scala特性的动机不同。

The former was introduced to support safe API evolution and a limited form of multiple inheritance. With leveraging functional programming idioms in Project Lambda it's been beneficial to add, for example, a forEach(lambda) method to java.util.Collection interface without altering all possible implementers (which is actually impossible to do without breaking backward compatibility). As a side effect this also offered a form of mixin composition.

前者是为了支持安全API的发展和有限形式的多重继承而引入的。通过在Project Lambda中使用函数式编程习惯用法,将forEach(Lambda)方法添加到java.util是很有好处的。集合接口不改变所有可能的实现者(实际上不破坏向后兼容性是不可能的)。作为副作用,这也提供了一种混合物成分的形式。

Scala traits were designed from scratch as building blocks for modular components composition. They are multiple inheritance friendly and don't have diamond problem by having strict rules on evaluation order of mix-ins due to linearization. They also support state, can reference the implementing class and place restrictions on which type can mix-in them. Look at Scala collections library where traits are used thoroughly.

Scala特性是作为模块化组件组合的构建模块从头设计的。它们具有多重继承友好性,且不存在因线性化而对混合优化排序有严格规则的菱形问题。它们还支持状态,可以引用实现类,并对可以混合的类型设置限制。看看Scala集合库,在那里可以彻底地使用特性。

#2


10  

Note that with scala 2.12.0 RC1 (Sept. 2016), Trait now compiles to an interface.
Scala 2.12 is all about making optimal use of Java 8’s new features

注意,使用scala 2.12.0 RC1(2016年9月),Trait现在可以编译成一个接口。Scala 2.12主要是为了优化使用Java 8的新特性

With Java 8 allowing concrete methods in interfaces, Scala 2.12 is able to compile a trait to a single interface.

通过Java 8允许在接口中使用具体的方法,Scala 2.12能够将一个特性编译为一个接口。

Before, a trait was represented as a class that held the method implementations and an interface.
Note that the compiler still has quite a bit of magic to perform behind the scenes, so that care must be taken if a trait is meant to be implemented in Java.
(Briefly, if a trait does any of the following its subclasses require synthetic code: defining fields, calling super, initializer statements in the body, extending a class, relying on linearization to find implementations in the right super trait.)

在此之前,一个特征被表示为持有方法实现和接口的类。请注意,编译器仍然有相当多的魔法可以在幕后执行,因此,如果要在Java中实现某个特性,就必须小心。(简单地说,如果一个特性做了以下任何一种事情,那么它的子类需要合成代码:定义字段、调用super、主体中的初始化语句、扩展类、依赖于线性化来找到正确的super trait的实现。)

See scala PR 5003 more the difference of implementation.

更多的见scala PR 5003实现的不同。

#1


33  

Motivations for Java 8' default methods and Scala traits differ.

Java 8的默认方法和Scala特性的动机不同。

The former was introduced to support safe API evolution and a limited form of multiple inheritance. With leveraging functional programming idioms in Project Lambda it's been beneficial to add, for example, a forEach(lambda) method to java.util.Collection interface without altering all possible implementers (which is actually impossible to do without breaking backward compatibility). As a side effect this also offered a form of mixin composition.

前者是为了支持安全API的发展和有限形式的多重继承而引入的。通过在Project Lambda中使用函数式编程习惯用法,将forEach(Lambda)方法添加到java.util是很有好处的。集合接口不改变所有可能的实现者(实际上不破坏向后兼容性是不可能的)。作为副作用,这也提供了一种混合物成分的形式。

Scala traits were designed from scratch as building blocks for modular components composition. They are multiple inheritance friendly and don't have diamond problem by having strict rules on evaluation order of mix-ins due to linearization. They also support state, can reference the implementing class and place restrictions on which type can mix-in them. Look at Scala collections library where traits are used thoroughly.

Scala特性是作为模块化组件组合的构建模块从头设计的。它们具有多重继承友好性,且不存在因线性化而对混合优化排序有严格规则的菱形问题。它们还支持状态,可以引用实现类,并对可以混合的类型设置限制。看看Scala集合库,在那里可以彻底地使用特性。

#2


10  

Note that with scala 2.12.0 RC1 (Sept. 2016), Trait now compiles to an interface.
Scala 2.12 is all about making optimal use of Java 8’s new features

注意,使用scala 2.12.0 RC1(2016年9月),Trait现在可以编译成一个接口。Scala 2.12主要是为了优化使用Java 8的新特性

With Java 8 allowing concrete methods in interfaces, Scala 2.12 is able to compile a trait to a single interface.

通过Java 8允许在接口中使用具体的方法,Scala 2.12能够将一个特性编译为一个接口。

Before, a trait was represented as a class that held the method implementations and an interface.
Note that the compiler still has quite a bit of magic to perform behind the scenes, so that care must be taken if a trait is meant to be implemented in Java.
(Briefly, if a trait does any of the following its subclasses require synthetic code: defining fields, calling super, initializer statements in the body, extending a class, relying on linearization to find implementations in the right super trait.)

在此之前,一个特征被表示为持有方法实现和接口的类。请注意,编译器仍然有相当多的魔法可以在幕后执行,因此,如果要在Java中实现某个特性,就必须小心。(简单地说,如果一个特性做了以下任何一种事情,那么它的子类需要合成代码:定义字段、调用super、主体中的初始化语句、扩展类、依赖于线性化来找到正确的super trait的实现。)

See scala PR 5003 more the difference of implementation.

更多的见scala PR 5003实现的不同。