【问题标题】:single and double cannot be augmented with a Measuresingle 和 double 不能用 Measure 增加
【发布时间】:2012-12-17 07:03:36
【问题描述】:

有谁知道为什么 [] 类型只增加了一些代数类型? 这么多有用的类型不能用 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

【问题讨论】:

    标签: f# units-of-measurement


    【解决方案1】:

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

    1. 似乎无法通过设计对无符号类型进行注释。我不知道为什么会这样,但是如果你尝试类似let _ = 1u&lt;1&gt; 这样的内容,你会得到一个非常有用的错误消息。
    2. 由于类型同义词的工作方式,您只能使用具有“真实”类型的度量,而不能使用它们的同义词。这就是为什么您可以使用带有float32float 的度量,但不能使用singledouble。请注意,int32 也是如此。

    【讨论】:

    • 如果有人不清楚,float32single 是同一个东西,floatdouble - 所以缺乏单双支持更多的是语义大于功能。
    【解决方案2】:

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

      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&lt;1&gt;,因为没有相关的类型。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-09-30
      • 2011-09-20
      • 1970-01-01
      • 1970-01-01
      • 2022-10-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多