3 people like it.

Generalized Units of Measure Revisited (using method overloading)

An approach to using annotated types in F# using overloaded methods.

Examples

1: 
2: 
3: 
4: 
5: 
6: 
[<Measure>] type m
[<Measure>] type n

let x = UnitOfMeasure.tag<m> "string"
let y = UnitOfMeasure.cast<m,n> x
let z = UnitOfMeasure.untag y
Multiple items
type MeasureAttribute =
  inherit Attribute
  new : unit -> MeasureAttribute

Full name: Microsoft.FSharp.Core.MeasureAttribute

--------------------
new : unit -> MeasureAttribute
[<Measure>]
type m

Full name: Script.m
[<Measure>]
type n

Full name: Script.n
val x : string<m>

Full name: Script.x
type UnitOfMeasure =
  static member cast : x:bool<'m1> -> bool<'m2>
  static member cast : x:int<'m1> -> int<'m2>
  static member cast : x:int64<'m1> -> int64<'m2>
  static member cast : x:uint64<'m1> -> uint64<'m2>
  static member cast : x:float<'m1> -> float<'m2>
  static member cast : x:decimal<'m1> -> decimal<'m2>
  static member cast : x:Guid<'m1> -> Guid<'m2>
  static member cast : x:string<'m1> -> string<'m2>
  static member cast : x:TimeSpan<'m1> -> TimeSpan<'m2>
  static member cast : x:DateTime<'m1> -> DateTime<'m2>
  ...

Full name: Script.UnitOfMeasure
static member UnitOfMeasure.tag : x:bool -> bool<'m>
   (+0 other overloads)
static member UnitOfMeasure.tag : x:int -> int<'m>
   (+0 other overloads)
static member UnitOfMeasure.tag : x:int64 -> int64<'m>
   (+0 other overloads)
static member UnitOfMeasure.tag : x:uint64 -> uint64<'m>
   (+0 other overloads)
static member UnitOfMeasure.tag : x:float -> float<'m>
   (+0 other overloads)
static member UnitOfMeasure.tag : x:decimal -> decimal<'m>
   (+0 other overloads)
static member UnitOfMeasure.tag : x:Guid -> Guid<'m>
   (+0 other overloads)
static member UnitOfMeasure.tag : x:string -> string<'m>
   (+0 other overloads)
static member UnitOfMeasure.tag : x:TimeSpan -> TimeSpan<'m>
   (+0 other overloads)
static member UnitOfMeasure.tag : x:DateTime -> DateTime<'m>
   (+0 other overloads)
val y : string<n>

Full name: Script.y
static member UnitOfMeasure.cast : x:bool<'m1> -> bool<'m2>
   (+0 other overloads)
static member UnitOfMeasure.cast : x:int<'m1> -> int<'m2>
   (+0 other overloads)
static member UnitOfMeasure.cast : x:int64<'m1> -> int64<'m2>
   (+0 other overloads)
static member UnitOfMeasure.cast : x:uint64<'m1> -> uint64<'m2>
   (+0 other overloads)
static member UnitOfMeasure.cast : x:float<'m1> -> float<'m2>
   (+0 other overloads)
static member UnitOfMeasure.cast : x:decimal<'m1> -> decimal<'m2>
   (+0 other overloads)
static member UnitOfMeasure.cast : x:Guid<'m1> -> Guid<'m2>
   (+0 other overloads)
static member UnitOfMeasure.cast : x:string<'m1> -> string<'m2>
   (+0 other overloads)
static member UnitOfMeasure.cast : x:TimeSpan<'m1> -> TimeSpan<'m2>
   (+0 other overloads)
static member UnitOfMeasure.cast : x:DateTime<'m1> -> DateTime<'m2>
   (+0 other overloads)
val z : string

Full name: Script.z
static member UnitOfMeasure.untag : x:bool<'m> -> bool
   (+0 other overloads)
static member UnitOfMeasure.untag : x:int<'m> -> int
   (+0 other overloads)
static member UnitOfMeasure.untag : x:int64<'m> -> int64
   (+0 other overloads)
static member UnitOfMeasure.untag : x:uint64<'m> -> uint64
   (+0 other overloads)
static member UnitOfMeasure.untag : x:float<'m> -> float
   (+0 other overloads)
static member UnitOfMeasure.untag : x:decimal<'m> -> decimal
   (+0 other overloads)
static member UnitOfMeasure.untag : x:Guid<'m> -> Guid
   (+0 other overloads)
static member UnitOfMeasure.untag : x:string<'m> -> string
   (+0 other overloads)
static member UnitOfMeasure.untag : x:TimeSpan<'m> -> TimeSpan
   (+0 other overloads)
static member UnitOfMeasure.untag : x:DateTime<'m> -> DateTime
   (+0 other overloads)
Next Version Raw view Test code New version

More information

Link:http://fssnip.net/7UH
Posted:6 years ago
Author:Eirik Tsarpalis
Tags: units of measure