F # also has a โcollapse your own styleโ method. Basically, your abstract type members (instance and static) become fields of the "typeclass" record, and the values โโof this record are instances of typeclass. You may have a copy of the euro, a copy of the dollar, etc .:
module Currency = type t<[<Measure>] 'a> = { zero : decimal<'a>; from : decimal -> decimal<'a> } /// Helper function to easily create typeclass instances for any /// currency. let make<[<Measure>] 'a> (curr_unit : decimal<'a>) : t<'a> = { zero = curr_unit - curr_unit; from = ((*) curr_unit) } [<Measure>] type euro let euro : t<euro> = make 1m<euro> [<Measure>] type dollar let dollar : t<dollar> = make 1m<dollar>
The unique thing about F # is that the type parameter that is passed to each instance of typeclass can actually be a measure type that is suitable for currencies.
source share