Rust和Haskell中的类型类的特性有什么区别? [英] What is the difference between traits in Rust and typeclasses in Haskell?

查看:142
本文介绍了Rust和Haskell中的类型类的特性有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Rust中的特质至少表面上类似于 typeclasses 在Haskell中,但是我看到有人写他们之间有一些区别。我想知道这些差异究竟是什么。

解决方案

在基本层面上,没有太大的区别,但他们仍然存在。
$ b Haskell将类型类中定义的函数或值描述为方法,正如特征描述它们所包含的对象中的OOP方法一样。然而,Haskell处理这些问题的方式不同,将它们视为单独的值,而不是将它们固定为一个对象,因为OOP会导致他们这样做。这是最明显的表面水平差异。



直到最近,Rust不能做的一件事是高阶类型特征,例如臭名昭​​着的 Functor Monad typeclasses。



这意味着Rust特征只能描述通常所说的具体类型,换句话说,没有泛型参数。然而,Haskell可以创建高阶类型类,它们使用类似于高阶函数使用其他函数的类型,用一个来描述另一个函数。我们无法实现Rust中前面提到的类型类型,因为它不支持这个在Haskell中被视为基本的特性,甚至是不可或缺的特性。谢天谢地,最近这个功能已经实现了相关项目。然而,这不是惯用的Rust,通常被认为是'黑客'。

同样值得一提的是,如评论中所述,GHC(Haskell的主编译器)支持类型类的更多选项,包括多参数< a>(即涉及的许多类型)类型类,以及函数依赖,这是一个可爱的选项,允许使用类型级别计算并引导到键入系列。据我所知,Rust既没有fundeps也没有类型的家族,尽管它可能在未来。†总而言之,虽然特征和类型类似于表面上的等价物,当考虑到许多特质时,他们确实有很多不同之处。如果考虑到很多特质的话,他们确实有很多不同之处。

*在附注中,Swift尽管有特质,但没有这种更高级的输入机制。未来的语言更新,也许呢?



†可以找到关于Haskell的类别类(包括更高类型的类)的一篇很好的文章这里,同样对Rust的特征也是一样的这里,但不幸的是已经过时了。


Traits in Rust seem at least superficially similar to typeclasses in Haskell, however I've seen people write that there are some differences between them. I was wondering exactly what these differences are.

解决方案

At the basic level, there's not much difference, but they're still there.

Haskell describes functions or values defined in a typeclass as 'methods', just as traits describe OOP methods in the objects they enclose. However, Haskell deals with these differently, treating them as individual values rather than pinning them to an object as OOP would lead one to do. This is about the most obvious surface-level difference there is.

The one thing that Rust could not do, up until recently, was higher-order typed traits, such as the infamous Functor and Monad typeclasses.

This means that Rust traits could only describe what's often called a 'concrete type', in other words, one without a generic argument. Haskell, however, can make higher-order typeclasses which use types similar to how higher-order functions use other functions, using one to describe another. We couldn't implement the typeclasses aforementioned in Rust, because it didn't support this feature that is regarded as basic, and even essential, in Haskell. Thankfully, this has been implemented recently with associated items.* However, this is not idiomatic Rust, and is generally considered a 'hack'.

It is also mentionable, as said in the comments, that GHC (Haskell's principal compiler) supports further options for typeclasses, including multi-parameter (i.e. many types involved) typeclasses, and functional dependencies, a lovely option that allows for type-level computations, and leads on to type families. To my knowledge, Rust has neither funDeps or type families, though it may in the future.†

All in all, whilst traits and typeclasses may seem equivalent on the surface, they do have a lot of differences when compared more deeply, when considering numerous qualities.


* On a side note, Swift, despite having traits, has no such higher-typing mechanism. A future update to the language, maybe?

† A nice article on Haskell's typeclasses (including higher-typed ones) can be found here, and an equally good one on Rust's traits is kept here, though is unfortunately a tad out-of-date.

这篇关于Rust和Haskell中的类型类的特性有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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