简体   繁体   English

为什么我不能将单位的参数传递给F#类型?

[英]Why can't I pass arguments with units to F# types?

Suppose I have defined a unit of measure: 假设我已经定义了一个度量单位:

[<Measure>] type Blob

And I want a type that takes a value in Blobs per second as a constructor argument: 我想要一个类型,以每秒Blobs的值作为构造函数参数:

type Something(flowRate:double<Blob/s>) = ...

F# throws a wobbly on this - "double does not expect any type arguments, but here is given 1 type argument" F#对此抛出一个摇摆不定 - “double不期望任何类型的参数,但这里给出了1个类型的参数”

I understand the message. 我理解这个消息。 I'd have thought it was obvious what I was trying to do, though I acknowledge that the syntax probably is verifiably wrong. 我曾经认为我试图做的很明显,但我承认语法可能是错误的。 Question is, how do I express this relationship in code? 问题是,我如何在代码中表达这种关系?

As the message (sort of) indicates, doubles aren't measure-generic. 正如消息(类型)所示,双打不是度量通用的。 Try float<Blob/s> instead. 尝试float<Blob/s> It's a bit strange, since float is a type synonym for type double . 这有点奇怪,因为floatdouble类型的同义词。 However, type float<[<Measure>]'t> is in some ways its own separate type. 但是,类型float<[<Measure>]'t>在某些方面是它自己的单独类型。 A similar problem occurs with single vs. float32 , int32 vs. int , and int8 vs. byte . singlefloat32int32int ,以及int8byte相似。 Section 9.7 of the spec partly covers this information. 规范的第9.7节部分涵盖了这些信息。

It's especially confusing since you can't define your own types which differ only in their measure arity: 这是特别令人困惑的,因为你不能定义自己的类型,只有它们的度量不同:

type T = class end
type T<[<Measure>]'t> = class end //' Duplicate definition of type T

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM