F# single和double不能用度量值进行扩充

F# single和double不能用度量值进行扩充,f#,units-of-measurement,F#,Units Of Measurement,有人知道为什么[]类型只增加了一些代数类型吗? 对我来说,这么多有用的类型不能用度量来扩充,这看起来很奇怪 type FooType = | FooByte of byte<1> // Error | FooSbyte of sbyte<1> // Ok | FooInt16 of int16<1> // Ok | FooUint16 of uint16<1>

有人知道为什么[]类型只增加了一些代数类型吗? 对我来说,这么多有用的类型不能用度量来扩充,这看起来很奇怪

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
类型FooType=
|FooByte of byte//错误
|FooSbyte of sbyte//Ok
|FooInt16/int16//Ok
|uint16的FooUint16//错误
|FooInt of int//Ok
|uint32的FooInt32//错误
|FooInt64/int64//Ok
|uint64的FooUint64//错误
|FoonNativeInt of nativeint//错误
|FooUnativeint of unativeint//错误
|FooChar of char//错误
|FooDecimal of decimal//Ok
|footfloat32/float32//Ok
|foosingofsingle//Error
|foodfloat的float//Ok
|foodoull of double//Error

这里有两个不同的问题:

  • 似乎设计无法对未签名类型进行注释。我不确定这是为什么,但如果您尝试类似于
    let\uu
    的方法,您可以得到一条信息丰富的错误消息
  • 由于类型同义词的工作方式,您只能对“真实”类型使用度量值,而不能对其同义词使用度量值。这就是为什么您可以对
    float32
    float
    使用度量,但不能对
    single
    double
    使用度量。请注意,
    int32也同样如此

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

      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 >
    
    sbyte//有符号字节
    int16
    int32
    int64
    ieee32//float32
    ieee64//浮点
    十进制
    
    这就是无符号类型没有度量值类型的原因。此外,从物理角度来看,几乎任何单位数量都可以是负数,因此这似乎是一个合理的选择

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

    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>
    
    F#core库定义了以下类型:
    类型浮动
    类型十进制
    sbyte型
    
    键入int64,如果任何人都不清楚,
    float32
    single
    是同一件事,就像
    float
    double
    一样,因此缺少single和double支持更多的是语义而不是功能。