5
GitHub - fsprojects/FSharp.UMX: F# units of measure for primitive non-numeric ty...
source link: https://github.com/fsprojects/FSharp.UMX
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
FSharp.UMX
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'
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK