Firstly, apologies for the bad title - I don't understand F # enough to better describe the problem.
Consider this simple DU:
type Money =
| USD of decimal
| GBP of decimal
| EUR of decimal
static member (+) (first: Money, second: Money) =
match first, second with
| USD(x), USD(y) -> USD(x + y)
| GBP(x), GBP(y) -> GBP(x + y)
| EUR(x), EUR(y) -> EUR(x + y)
| _ -> failwith "Different currencies"
I represent money in different currencies and overloading the operator (+) so that I can safely make money + money. However, if I have a lot of currencies, then the statement of conformity will be tedious to write. Is there a way to express something like:
match first, second with
| _(x), _(y) -> _(x + y)
Or is there another way to achieve the same result? I reviewed and discarded units due to the limitations described here .
source
share