小编Ces*_*ata的帖子

为什么使用“Self”作为参数类型会引发生命周期错误?

我目前正在关注https://raytracing.github.io/books/RayTracingInOneWeekend.html,但我正在 Rust 中实现所有内容。这是我的向量实现的摘录:

type Scalar = f64;

#[derive(Debug, Default, Clone)]
pub struct Vector {
    x: Scalar,
    y: Scalar,
    z: Scalar,
}

impl Vector {
    fn new(x: Scalar, y: Scalar, z: Scalar) -> Self {
        Self { x, y, z }
    }

    fn x(&self) -> Scalar {
        self.x
    }

    fn y(&self) -> Scalar {
        self.y
    }

    fn z(&self) -> Scalar {
        self.z
    }
}

impl std::ops::Mul<&Vector> for &Vector {
    type Output = Scalar;

    fn mul(self, rhs: Self) -> Self::Output …
Run Code Online (Sandbox Code Playgroud)

type-inference lifetime rust

8
推荐指数
1
解决办法
94
查看次数

使用uncurry函数的特定类型推断

我一直在玩uncurryGHCi 的功能,我发现了一些我根本无法得到的东西.当我申请uncurry(+)功能,并结合该像在下面的代码中的一些变量,所述编译器推断它的类型是特定于Integer:

Prelude> let add = uncurry (+)
Prelude> :t add
add :: (Integer, Integer) -> Integer
Run Code Online (Sandbox Code Playgroud)

但是,当询问以下表达式的类型时,我得到(我期望的)正确的结果:

Prelude> :t uncurry (+)
uncurry (+) :: (Num a) => (a, a) -> a
Run Code Online (Sandbox Code Playgroud)

会导致什么?这是GHCi特有的吗?

这同样适用于let add' = (+).

注意:我无法使用编译文件重现它.

haskell types ghc monomorphism-restriction

6
推荐指数
1
解决办法
359
查看次数