Is it possible to make codecs to automatically handle units of measure?
See original GitHub issueHere are two types (X and Y) with units of measure.
get_Codec
shows how I’m trying to handle UOM now, and get_Codec2
shows what would I would like to be able to write, if there was a way to handle UOM automatically.
Obviously it’s not too bad for X
, but it’s pretty ugly (and inefficient) for things like Y
.
Is there a way the codecs could handle UOM on numerics (and preferably string too preferably, as used in FSharp.UMX) or is it possible to write a Codec that handles the units and defers to the Codecs for the primitives?
#r "nuget:Fleece"
#r "nuget:FSharp.UMX"
open Fleece
open FSharp.UMX
open FSharpPlus.Operators
type [<Measure>] m
type [<Measure>] n
type X = { A : float<m> } with
static member get_Codec () =
(fun a -> { A = %a })
<!> jreq "a" ((fun x -> x.A) >> UMX.untag >> Some)
|> ofObjCodec
//static member get_Codec2 () =
// (fun a -> { A = a })
// <!> jreq "a" ((fun x -> x.A) >> Some)
// |> ofObjCodec
type Y = { B : Map<int<n>, float<m>> } with
static member get_Codec () =
(fun b -> { B = (b |> Map.toSeq |> Seq.map (fun (k,v) -> (UMX.tag k, UMX.tag v)) |> Map.ofSeq) })
<!> jreq "b" (fun x -> x.B |> Map.toSeq |> Seq.map (fun (k,v) -> (UMX.untag k, UMX.untag v)) |> Map.ofSeq |> Some)
|> ofObjCodec
//static member get_Codec2 () =
// (fun b -> { B = b })
// <!> jreq "b" ((fun x -> x.B) >> Some)
// |> ofObjCodec
Any advice would be greatly appreciated.
Issue Analytics
- State:
- Created a month ago
- Reactions:1
- Comments:8 (4 by maintainers)
Top Results From Across the Web
Strategy to use two different measurement systems in ...
The best way to handle multiple conversions like that is to create a Unit of Measurement type class for your application.
Read more >MediaCodec
Build apps that give your users seamless experiences from phones to tablets, watches, and more. ... Learn to build for your use case...
Read more >Stay safe with your units! Advanced units of measure in .NET.
This mini-article shows a concept of advanced units of measure, fully type safe, with automatic unit conversion, without runtime dispatch, ...
Read more >LLNL/units: A run-time C++ library for working with ...
A run-time C++ library for working with units of measurement and conversions between them and with string representations of units and measurements -...
Read more >How to Set Up Item Units of Measure - Business Central
Assign alternate units of measure to purchase, production, or sales documents to specify how many units of the base unit of measure you...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
The definitions here might be kludgy because I don’t fully understand how all the codecs fit together, but I can add
Which allows me to express the codec for the simple record X as
But presumably that doesn’t make the codec for
{ B : Map<int<n>, float<m>> }
any easier?Based on what you have done above, I’ve started to try to decompose what is needed in order to define the different parts
this implies that if you could have a composed map codec you could do the same type of process.