有内在的原因可以解释为什么Rust没有更高种类的类型吗? [英] Is there an intrinsic reason explaining why Rust does not have higher-kinded-types?

查看:108
本文介绍了有内在的原因可以解释为什么Rust没有更高种类的类型吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

铁锈没有更高种类的类型.例如,函子(以及因此的monad)不能用Rust编写.我想知道是否有深层原因来解释这一点以及原因.

Rust does not have higher-kinded-types. For example, functor (and thus monad) cannot be written in Rust. I would like to know if there is a deep reason explaining this and why.

例如,我能理解的原因可能是没有零成本的抽象使HKT成为可能.否则类型推断要困难得多.当然,我也正在寻找一种解释,向我说明为什么这是真正的局限性.

For instance, reason that I can understand can be that there is no zero-cost abstraction making HKT possible. Or type inference is significantly more difficult. And of course, I also looking for an explaination showing me why this is a real limitation.

如果答案已经在其他地方给出了,您能给我链接吗?

If the anwer was already given somewhere else, could you give me the link?

推荐答案

时间&优先级.

缺少高级种类类型本身并不是设计决定.预期Rust将具有某种形式,更流行的候选者是通用关联类型(2017).

The absence of Higher Kinded Types is not a design decision, per se. It is intended that Rust will have some form of it, with the more popular candidate being Generic Associated Types (2017) at the moment.

但是,实现这些功能需要花费时间,与其他功能相比,还没有被确定为优先事项.例如,异步/等待优先于HKT,而const泛型似乎也优先.

Implementing those take time, though, and has not been judged a priority compared to other features. For example, async/await was prioritized over HKTs, and const generics also seem to be prioritized.

例如,函子(以及单子)不能用Rust编写.

For example, functor (and thus monad) cannot be written in Rust.

实际上,尽管有些麻烦,他们还是可以的.

Actually, they can, although it's a bit unwieldy.

请参见爱德蒙的史密斯可爱的黑客,他在通过它们实现 Functor 特性:

pub trait Functor: Unplug + Plug<<Self as Unplug>::A> {
    fn map<B, F>(f: F, s: Self) -> <Self as Plug<B>>::result_t
        where
            Self: Plug<B>,
            F: FnMut(<Self as Unplug>::A) -> B
        ;
}

//  Example impl for a represented Vec
impl<A> Functor for Concrete<Vec<forall_t>, A> {
    //  remember, Self ~ (Vec<_>, A) ~ "f a"
    fn map<B, F>(f: F, s: Self) -> <Self as Plug<B>>::result_t
        where
            F: FnMut(<Self as Unplug>::A) -> B 
    {        
        Concrete::of(s.unwrap.into_iter().map(f).collect())
    }
}

然后从此构建 Applicative Monad :

pub trait Applicative: Functor {
    fn pure(s: <Self as Unplug>::A) -> Self;

    fn app<B, F>(
        f: <Self as Plug<F>>::result_t, //M<F>
        s: Self                         //M<A>
    ) -> <Self as Plug<B>>::result_t   //M<B>
    where
        F: FnMut(<Self as Unplug>::A) -> B + Clone,
        Self: Plug<F> + Plug<B> + Unplug,
        <Self as Plug<F>>::result_t:
            Unplug<F=<Self as Unplug>::F, A=F> +
            Plug<F> +
            Clone,
        <Self as Unplug>::F: Plug<F>
    ;
}

pub trait Monad : Applicative {
    fn bind<F,B>(f: F, s: Self) -> <Self as Plug<B>>::result_t
    where
        Self: Plug<F>+Plug<B>,
        F: FnMut(<Self as Unplug>::A) ->
            <Self as Plug<B>>::result_t + Clone
        ;
}

我确实说这有点笨拙...

这篇关于有内在的原因可以解释为什么Rust没有更高种类的类型吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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