4

有谁知道为什么 [<Measure>] 类型只增加了一些代数类型?这么多有用的类型不能用 Measure 来扩充,这对我来说很奇怪。

type FooType =
| FooByte       of byte<1>          // Error
| FooSbyte      of sbyte<1>         // Ok
| FooInt16      of int16<1>         // Ok
| FooUint16     of uint16<1>        // Error
| FooInt        of int<1>           // Ok
| FooInt32      of uint32<1>        // Error
| FooInt64      of int64<1>         // Ok
| FooUint64     of uint64<1>        // Error
| FooNativeint  of nativeint<1>     // Error
| FooUnativeint of unativeint<1>    // Error
| FooChar       of char<1>          // Error
| FooDecimal    of decimal<1>       // Ok
| FooFloat32    of float32<1>       // Ok
| FooSingle     of single<1>        // Error
| FooFLoat      of float<1>         // Ok
| FooDouble     of double<1>        // Error
4

2 回答 2

5

好吧,这里有两个不同的问题:

  1. 似乎无符号类型无法通过设计进行注释。我不确定这是为什么,但是如果您尝试类似let _ = 1u<1>.
  2. 由于类型同义词的工作方式,您只能使用具有“真实”类型的度量,而不能使用它们的同义词。这就是为什么您可以使用带有float32和的度量float,但不能使用singledouble。请注意,同样如此int32
于 2013-01-02T22:23:49.047 回答
4

根据规范,只有以下文字类型可以有度量

  sbyte < measure-literal > //signed byte
  int16 < measure-literal >
  int32 < measure-literal >
  int64 < measure-literal >
  ieee32 < measure-literal > //float32
  ieee64 < measure-literal > //float
  decimal < measure-literal >

所以这就是无符号类型没有度量类型的原因。此外,从物理角度来看,几乎任何带有单位的数量都可以是负数,所以这似乎是一个合理的选择。

其余部分不起作用的原因来自规范:

The F# core library defines the following types:

type float<[<Measure>] 'U>
type float32<[<Measure>] 'U>
type decimal<[<Measure>] 'U>
type int<[<Measure>] 'U>
type sbyte<[<Measure>] 'U>
type int16<[<Measure>] 'U>
type int64<[<Measure>] 'U>

这就是为什么你不能这样做single<1>,因为没有相关的类型。

于 2013-01-02T22:37:58.910 回答