Java的接口和Haskell的类型类:差异和相似之处? [英] Java's Interface and Haskell's type class: differences and similarities?

查看:105
本文介绍了Java的接口和Haskell的类型类:差异和相似之处?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我学习Haskell时,我注意到它的 type class ,它应该是一个源自Haskell的伟大发明。



但是,在类型类上的维基百科页面:


程序员通过指定一组函数或
常量名称以及它们各自的类型来定义一个类型类别,对于属于该类别的每个类型必须存在
。 p>

对我来说,这似乎与 Java的界面非常接近(引用维基百科的界面(Java)页面):


Java编程语言是一种抽象类型,
用于指定类必须实现的接口(在术语的通用意义上)


这两个看起来相当类似lar:类型限制类型的行为,而接口限制类的行为。



我想知道Haskell中的类型和Java中的接口有什么不同和相似之处,或者他们根本不同?



编辑:我注意到甚至haskell.org也承认它们是相似的。如果他们是如此相似(或他们?),那么为什么类型类会被这样的炒作对待?

更多编辑:许多伟大的答案!我想我必须让社区决定哪一个最好。然而,在阅读答案时,他们似乎都只是说类接口可以做很多事情,而接口不能或不得不应对泛型。我不禁想知道,有什么接口可以做到,而类型类不能呢?另外,我注意到维基百科声称类型类最初是在1989年的论文中发明的*如何使ad-hoc多态减少广告特别,而哈斯克尔仍然处于摇篮中,而Java项目于1991年开始,并于1995年首次发布。因此,也许不是类型接口相似,反之,接口受类型类型影响? 有没有文件/文件支持或反驳这个?感谢所有的答案,他们都非常有启发性!



感谢您的所有投入!

解决方案

我会说接口有点像类型类 SomeInterface t ,其中所有值的类型都是 t - >无论(其中 whatever 不包含 t )。这是因为有了Java和类似语言中的继承关系,调用的方法取决于它们被调用的对象的类型,而不是其他任何东西。



很难做出像 add :: t - >这样的东西。 t - > t 带有一个接口,它在多于一个参数上是多态的,因为接口没有办法指定方法的参数类型和返回类型是与类型相同的类型它被调用的对象(即自我类型)。对于泛型,有一些方法可以通过使用与对象本身相同类型的泛型参数来创建接口,如 Comparable< T> 它在你希望使用的地方 Foo实现了Comparable< Foo> ,因此 compareTo(T其他)有类型 t - > t - >排序。但是这仍然需要程序员遵循这个规则,并且当人们想要使用这个接口的函数时也会引起头痛,他们必须具有递归泛型类型参数。



另外,你不会有像 empty :: t 这样的东西,因为你不在这里调用函数,所以它不是一个方法。


While I am learning Haskell, I noticed its type class, which is supposed to be a great invention that originated from Haskell.

However, in the Wikipedia page on type class:

The programmer defines a type class by specifying a set of function or constant names, together with their respective types, that must exist for every type that belongs to the class.

Which seems rather close to Java's Interface to me (quoting Wikipedia's Interface(Java) page):

An interface in the Java programming language is an abstract type that is used to specify an interface (in the generic sense of the term) that classes must implement.

These two looks rather similar: type class limit a type's behavior, while interface limit a class' behavior.

I wonder what are the differences and similarities between type class in Haskell and interface in Java, or maybe they are fundamentally different?

EDIT: I noticed even haskell.org admits that they are similar. If they are so similar (or are they?), then why type class is treated with such hype?

MORE EDIT: Wow, so many great answers! I guess I'll have to let the community decide which is the best one. However, while reading the answers, all of them seem to just say that "there are many things typeclass can do while interface cannot or have to cope with generics". I cannot help but wondering, are there anything interfaces can do while typeclasses cannot? Also, I noticed that Wikipedia claims that typeclass was originally invented in the 1989 paper *"How to make ad-hoc polymorphism less ad hoc", while Haskell is still in its cradle, while Java project was started in 1991 and first released in 1995. So maybe instead of typeclass being similar to interfaces, its the other way around, that interfaces were influenced by typeclass? Are there any documents/papers support or disprove this? Thanks for all the answers, they are all very enlightening!

Thanks for all the inputs!

解决方案

I would say that an interface is kind of like a type class SomeInterface t where all of the values have the type t -> whatever (where whatever does not contain t). This is because with the kind of inheritance relationship in Java and similar languages, the method called depends on the type of object they are called on, and nothing else.

That means it's really hard to make things like add :: t -> t -> t with an interface, where it is polymorphic on more than one parameter, because there's no way for the interface to specify that the argument type and return type of the method is the same type as the type of the object it is called on (i.e. the "self" type). With Generics, there are kinda ways to fake this by making an interface with generic parameter that is expected to be the same type as the object itself, like how Comparable<T> does it, where you are expected to use Foo implements Comparable<Foo> so that the compareTo(T otherobject) kind of has type t -> t -> Ordering. But that still requires the programmer to follow this rule, and also causes headaches when people want to make a function that uses this interface, they have to have recursive generic type parameters.

Also, you won't have things like empty :: t because you're not calling a function here, so it isn't a method.

这篇关于Java的接口和Haskell的类型类:差异和相似之处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆