【问题标题】:Making a F# type a measure as well?使 F# 类型也成为度量?
【发布时间】:2020-05-27 12:01:33
【问题描述】:

我正在阅读 Friendly F#,书中有这段代码

[<Measure>]
type Vector2<[<Measure>]'a> =
{
   X: float<'a>
   Y: float<'a>
}

因此,此代码旨在与 F# 和 VS 2010 一起使用。我目前正在使用 VS2012。

VS2012 显示错误。这是 F# 版本的问题吗?如果是这样,正确的方法是什么?

它显示的错误是:

Error 4 A type definition requires one or more members or other declarations. If you intend to define an empty class, struct or interface, then use 'type ... = class end', 'interface end' or 'struct end'.

【问题讨论】:

  • 有什么问题?
  • 2012年,代码显示错误。我应该把它放在 OP 中。
  • 如果您正在运行书中的小示例,我建议您尝试 VSCode + Ionide。它对 F# 脚本具有出色的支持,这意味着您可以通过将代码直接发送到 REPL 来执行代码,绕过任何编译步骤。 ionide.io

标签: types f# units-of-measurement


【解决方案1】:

看起来像一个缩进问题:

type Vector2<[<Measure>]'a> =
    {
       X: float<'a>
       Y: float<'a>
    }

[<Measure>] type kg

{ X = 13.<kg> ; Y = 10.<_> }

// val it : Vector2<kg> = { X = 13.0; Y = 10.0 }

【讨论】:

  • 你完全正确。我的代码没有任何问题,因为上面的 Measure 属性也导致了错误。
猜你喜欢
  • 2011-04-17
  • 2018-03-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-26
  • 2012-05-25
相关资源
最近更新 更多