Can you suggest a best way to define money type in F#?
use a long, and store pennies (or tenths of a penny) in it.
You could use a class like Decimal, but that usually ends up being quite slow.
Always, always, use System.Decimal for storing financial data! (Unless you dont care about inaccuracy and rounding errors!) http://msdn.microsoft.com/en-us/library/364x0z75(VS.71).aspx
type money = int<dollars>
?
Haven't even tried it to see... can you define arbitrary units, or does it only work with explicitly defined ones?
Obviously you'd probably want
type money = int<thousandths_of_currency>
(or tens of pennies, or whatever).
To be more accurate.
edited:
decimals take types so you can define money as:
[<Measure>]
type = pounds
type money = decimal<pounds>
which could ensure currencies aren't cross converted by accident, eg:
if
balance = decimal<pounds>
and
payment = decimal<dollars>
newbalance = balance + payment
will not compile, and you'll have to convert payment to decimal<pounds>
Luca Bolognese suggests one defines their own Money type based off of float:
[<Measure>] type money
let money (f:float) = f * 1.<money>
F# now has built in support for Measures and Units. According to the lead engineer for this feature, Kennedy it is aimed at financial apps among other solutions.
So I would look at that before defining my own money type in F#.
Werner