Single and double cannot be supplemented by measure

Does anyone know why [<Measure>] only increase some algebraic types? The fact that so many useful types cannot be supplemented by a measure looks strange to me.

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
source share
2 answers

Well, there are two different questions here:

  • It seems that unsigned types cannot be annotated by design. I'm not sure why this is so, but you can get a pretty informative error message if you try something like let _ = 1u<1> .
  • Because of the way type synonyms work, you can only use measures with "real" types, not with their synonyms. This is why you can use dimensions with float32 and float , but not single and double . Note that the same is true for int32 .
+5
source

According to the specification, only the following types of literals can have values

  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 > 

This is why unsigned types do not have measure types. In addition, to form a physical perspective, almost any value with units can be negative, so this seems like a reasonable alternative.

The reason the others are not working is the specification:

 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> 

therefore, you cannot do single<1> since there is no corresponding type.

+3
source

All Articles