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

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

问题描述

特征在 Rust 中似乎至少在表面上类似于 typeclasses ,但是我看到人们写道它们之间存在一些差异.我想知道这些差异到底是什么.

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 将类型类中定义的函数或值描述为方法",就像特征描述它们所包含的对象中的 OOP 方法一样.然而,Haskell 以不同的方式处理这些,将它们视为单独的值,而不是像 OOP 那样将它们固定到一个对象上.这是最明显的表面水平差异.

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.

Rust 暂时不能做的一件事是高阶类型特征,例如臭名昭​​著的 FunctorMonad 类型类.

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

这意味着 Rust trait 只能描述通常称为具体类型"的东西,换句话说,没有泛型参数的类型.Haskell 从一开始就可以制作使用类型的高阶类型类,类似于高阶函数使用其他函数的方式:使用一个来描述另一个.有一段时间,这在 Rust 中是不可能的,但是由于 相关项目 已经实现,这样的特征已经变得司空见惯.

This means that Rust traits could only describe what's often called a 'concrete type', in other words, one without a generic argument. Haskell from the start could make higher-order typeclasses which use types similar to how higher-order functions use other functions: using one to describe another. For a period of time this was not possible in Rust, but since associated items have been implemented, such traits have become commonplace and idiomatic.

因此,如果我们忽略扩展,它们并不完全相同,但每个都可以近似于另一个可以做什么.

So if we ignore extensions, they are not exactly the same, but each can approximate what the other can do.

还值得一提的是,正如评论中所说,GHC(Haskell 的主要编译器)支持类型类的更多选项,包括 多参数(即涉及多种类型)类型类,以及函数依赖,一个允许类型级计算的可爱选项,并导致类型系列.据我所知,Rust 既没有 funDeps 也没有类型族,尽管将来可能会有.†

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.†

总而言之,traits 和 typeclass 有着根本的区别,这取决于它们交互的方式,使它们的行为和最终看起来非常相似.

All in all, traits and typeclasses have fundamental differences, which due to the way they interact, make them act and seem quite similar in the end.

† 可以在这里找到一篇关于 Haskell 类型类(包括高级类型)的好文章a> 和 Rust by Example 章节中关于 trait 的内容可以在 这里找到

† A nice article on Haskell's typeclasses (including higher-typed ones) can be found here, and the Rust by Example chapter on traits may be found here

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

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