Guest User

Untitled

a guest
Dec 14th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.27 KB | None | 0 0
  1. #nowarn "42"
  2.  
  3. open System
  4.  
  5. [<MeasureAnnotatedAbbreviation>] type bool<[<Measure>] 'm> = bool
  6. [<MeasureAnnotatedAbbreviation>] type uint64<[<Measure>] 'm> = uint64
  7. [<MeasureAnnotatedAbbreviation>] type Guid<[<Measure>] 'm> = Guid
  8. [<MeasureAnnotatedAbbreviation>] type string<[<Measure>] 'm> = string
  9. [<MeasureAnnotatedAbbreviation>] type TimeSpan<[<Measure>] 'm> = TimeSpan
  10. [<MeasureAnnotatedAbbreviation>] type DateTime<[<Measure>] 'm> = DateTime
  11. [<MeasureAnnotatedAbbreviation>] type DateTimeOffset<[<Measure>] 'm> = DateTimeOffset
  12.  
  13. type UnitOfMeasureTC =
  14. // Method signatures declare a type relationship between
  15. // units of measure of the same underlying type
  16. // NB underlying types in UoM arguments should always match
  17. static member IsUnitOfMeasure(_ : bool<'a>, _ : bool<'b>) = ()
  18. static member IsUnitOfMeasure(_ : int<'a>, _ : int<'b>) = ()
  19. static member IsUnitOfMeasure(_ : int64<'a>, _ : int64<'b>) = ()
  20. static member IsUnitOfMeasure(_ : uint64<'a>, _ : uint64<'b>) = ()
  21. static member IsUnitOfMeasure(_ : float<'a>, _ : float<'b>) = ()
  22. static member IsUnitOfMeasure(_ : decimal<'a>, _ : decimal<'b>) = ()
  23. static member IsUnitOfMeasure(_ : Guid<'a>, _ : Guid<'b>) = ()
  24. static member IsUnitOfMeasure(_ : string<'a>, _ : string<'b>) = ()
  25. static member IsUnitOfMeasure(_ : TimeSpan<'a>, _ : TimeSpan<'b>) = ()
  26. static member IsUnitOfMeasure(_ : DateTime<'a>, _ : DateTime<'b>) = ()
  27. static member IsUnitOfMeasure(_ : DateTimeOffset<'a>, _ : DateTimeOffset<'b>) = ()
  28.  
  29. [<RequireQualifiedAccess>]
  30. module UnitOfMeasure =
  31.  
  32. #if !FABLE_COMPILER
  33. let inline private _cast< ^TC, ^a, ^b when (^TC or ^a or ^b) : (static member IsUnitOfMeasure : ^a * ^b -> unit)> (t : ^a) = (# "" t : ^b #)
  34.  
  35. /// generic unit of measure cast function
  36. let inline cast (x : 'a) : 'b = _cast<UnitOfMeasureTC,'a,'b> x
  37. #else
  38. let inline cast (x : 'a) : 'b = unbox x
  39. #endif
  40.  
  41. [<Measure>] type dir
  42. [<Measure>] type file
  43.  
  44. let mkDir (x: string): string<dir> = UnitOfMeasure.cast x
  45. let mkFile (x: string): string<file> = UnitOfMeasure.cast x
  46.  
  47. let test (dirPath: string<dir>) (fileName: string<file>): unit =
  48. printfn "FULL PATH: %O/%O" dirPath fileName
  49.  
  50. let myDir = mkDir "/my/path"
  51. let myFile = mkFile "file.txt"
  52.  
  53. test myDir myFile
  54. // test myFile myDir // This doesn't compile
Add Comment
Please, Sign In to add comment