F# units of measure for primitive non-numeric types
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src Publish 1.0.0-preview-001 Feb 5, 2019
tests Rename to FSharp.UMX Feb 5, 2019
.gitignore First release Jan 24, 2019
Build.fsx
FSharp.UMX.sln Rename to FSharp.UMX Feb 5, 2019
LICENSE Publish 1.0.0-preview-001 Feb 5, 2019
Makefile
README.md
RELEASE_NOTES.md
Settings.FSharpLint
package-lock.json Publish 1.0.0-preview-001 Feb 5, 2019
package.json First release Jan 24, 2019

README.md

FSharp.UMX release NuGet code size license

F# Units of Measure for primitive non-numeric types by Eirik Tsarpalis. Compatible with Fable.

Installing

Add the FSharp.UMX package from Nuget or just copy the src/FSharp.UMX.fs file.

Publishing to Nuget

Run npm i && npm run build publish.

Usage

open FSharp.UMX

[<Measure>] type customerId
[<Measure>] type orderId
[<Measure>] type kg

type Order =
    {
        id : string<orderId>
        customer : string<customerId>
        quantity : int<kg>
    }

let order =
    {
        id = % "orderId"
        customer = % "customerId"
        quantity = % 42
    }

let printOrder (order : Order) =
    printfn "orderId=%s customerId=%s quantity=%d" %order.id %order.customer %order.quantity

let lookupById (orders : Order list) (id : string<orderId>) = orders |> List.tryFind (fun o -> o.id = id)

lookupById [] order.id // compiles
lookupById [] order.customer // compiler error
// stdin(94,15): error FS0001: Type mismatch. Expecting a
//     'string<orderId>'
// but given a
//     'string<customerId>'
// The unit of measure 'orderId' does not match the unit of measure 'customerId'

Example in Fable REPL