I am writing a conversion function for units of measurements in F#. The information regarding the values is coming from an ASP.NET web application.
The function I am creating is defined like this:
let convert (x:float<_>) (toType : string) =
// Do conversion here.
The first problem I am running into involves the toType : string definition. Is there any way I can make define it to accept a float<_> type…particularly coming from the web application? (I know it’s part of the CLR based on Andrew Kennedy’s excellent articles, but I don’t see how to generically define it in the function.
Thanks for any help you can provide.
If you just want to be able to read in “2.0 ft” and “5.0 m” then
you should write code that parses the strings and does some conditional logic (e.g. if “ft” then turn into
float<ft>or whatever)once you have the data converted into the F# type system you can do things in a typesafe way
This is typical of every app that wants to get strongly-typed data in from the outside world; there is an input component that must parse and validate the input and store it into a strongly-typed data structure, but then the program can use that data henceforth.
(There’s no magical parser/input validator in any library, you just do it for your data types.)