Skip to content

Instantly share code, notes, and snippets.

@undeadcat
Last active August 29, 2015 14:15
Show Gist options
  • Save undeadcat/3916cc75cd3263ff37b8 to your computer and use it in GitHub Desktop.
Save undeadcat/3916cc75cd3263ff37b8 to your computer and use it in GitHub Desktop.
Units of measure magic
let inline retype (t : ^t) :^tm = (# "" t : ^tm #)
let inline (++) (w : ^W when ^W : (static member IsMeasureAbbrev : ^tm * ^t -> unit)) (t : ^t) = (# "" t : ^tm #)
open System
[<MeasureAnnotatedAbbreviation>]
type Guid<[<Measure>] 'Measure> = Guid
[<MeasureAnnotatedAbbreviation>]
type string<[<Measure>] 'Measure> = string
type UoM =
| UoM
static member IsMeasureAbbrev(_ : Guid<'Measure>, _ : Guid) = ()
static member IsMeasureAbbrev(_ : string<'Measure>, _ : string) = ()
[<Measure>]
type processId
[<Measure>]
type taskId
type Entry =
{ ProcessId : Guid<processId>
TaskId : Guid<taskId> }
let (taskId : Guid<taskId>) = UoM ++ Guid.NewGuid()
let (pid : Guid<processId>) = UoM ++ Guid.NewGuid()
let entry =
{ ProcessId = pid
TaskId = taskId }
[<EntryPoint>]
let main argv =
let (taskId : Guid<taskId>) = UoM ++ Guid.NewGuid()
let (pid : Guid<processId>) = UoM ++ Guid.NewGuid()
let good ={ProcessId = retype (Guid.NewGuid()); TaskId = retype (Guid.NewGuid())}
let evil ={ProcessId = retype (Guid.NewGuid()); TaskId = retype (false)}
printfn "%A" argv
0 // return an integer exit code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment