在 F# 中将向量与测量单位相乘

发布于 2024-12-18 11:07:36 字数 639 浏览 1 评论 0原文

我已经编写了以下内容

[<Measure>]
type m

[<Measure>]
type s

[<Measure>]
type v = m/s

type Vector3<[<Measure>] 'a> =
    {
    X : float<'a>
    Y : float<'a>
    Z : float<'a>
    }
    static member (*)
        (v:Vector3<'a>,f:float<'b>):Vector3<'a*'b> =
        { X = v.X*f; Y = v.Y*f ; Z = v.Z * f}

现在我尝试以这种方式使用它:

let next_pos (position:Vector3<m> , velocity: Vector3<m/s> ,dt : float<s>  ->  Vector3<m>) =
     position + (velocity * dt)

它给了我一个编译器错误,但我很确定度量单位的表达是正确的。 我有什么错?

I've written the following

[<Measure>]
type m

[<Measure>]
type s

[<Measure>]
type v = m/s

type Vector3<[<Measure>] 'a> =
    {
    X : float<'a>
    Y : float<'a>
    Z : float<'a>
    }
    static member (*)
        (v:Vector3<'a>,f:float<'b>):Vector3<'a*'b> =
        { X = v.X*f; Y = v.Y*f ; Z = v.Z * f}

Now I'm trying to use it this way:

let next_pos (position:Vector3<m> , velocity: Vector3<m/s> ,dt : float<s>  ->  Vector3<m>) =
     position + (velocity * dt)

It gives me a compiler error, but I'm pretty sure the measure unit are expressed right.
What's my mistake?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

靑春怀旧 2024-12-25 11:07:36

您尝试用于指定返回类型的语法不正确。它应该如下所示:

let next_pos (position:Vector3<m>, velocity:Vector3<m/s>, dt:float<s>) : Vector3<m> = 
  position + (velocity * dt) 

要指定函数返回 Vector3 类型的值,您需要向结果添加类型注释,这是通过编写 let foo来完成的。论据> : T = <表达式>。当向参数添加类型注释时,需要将它们放在括号中(这样语法才不会产生歧义)。正如 Paolo 在评论中指出的那样,您使用 -> 是说 dt 是一个函数,因为注释 float -> Vector3附加到参数dt

为了使代码编译,我还必须将 (+) 运算符的实现添加到您的 Vector3 中,但我假设您已经有了(并且在发布问题)。

The syntax you tried to use for specifying the return type was incorrect. It should look like this:

let next_pos (position:Vector3<m>, velocity:Vector3<m/s>, dt:float<s>) : Vector3<m> = 
  position + (velocity * dt) 

To specify that the function returns a value of type Vector3<m>, you need to add type annotation to the result, which is done by writing let foo <arguments> : T = <expr>. When adding type annotations to parameters, these need to be parenthesized (so the syntax is not ambiguous). As noted by Paolo in a comment, your use of -> was saying that dt is a function, because the annotation float<s> -> Vector3<m> was attached to the parameter dt.

To make the code compile, I also had to add an implementation of (+) operator to your Vector3, but I assume you have that already (and just left it out when posting the question).

假情假意假温柔 2024-12-25 11:07:36

我是这样解决的(但我不确定原因)。

let next_pos (position:Vector3<m> , velocity: Vector3<m/s> ,dt : float<s> ) =
     position + (velocity * dt)

如果我显式定义返回类型,编译器似乎会失败。如果我删除它似乎无论如何都能推断出正确的类型。
但为什么会这样呢?

除此之外,在某些情况下,类型声明中的名称冲突迫使我明确指定返回类型。所以我认为这最终不是正确的解决方案。

I solved this way (but I'm not sure about the reason).

let next_pos (position:Vector3<m> , velocity: Vector3<m/s> ,dt : float<s> ) =
     position + (velocity * dt)

It seems that the compiler fails if I explicetly define the return type. If I remove it seems able to infer the correct type anyway.
But why this?

In addition to that, there are situations in which name clashes in type declaration force me to explictely specify the return type. So I don't think this is the right solution in the end.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文