One of the great things about F# it that it allows you to annotate values with Units of Measure. (Remember the Mars Climate Orbiter burning up because all subcontractors used metric except Lockheed Martin?) F# will do limited testing to make sure your units are consistent. Units of Measure doesn't really understand the concepts of length, volume, force, or currency, but used wisely can be a great help. A little example:
[<Measure>] type cm [<Measure>] type inch val cmPerInch : float<cm/inch> = 2.54 > let length1 = 1.0<cm> let length2 = 1.0<inch>;; val length1 : float<cm> = 1.0 val length2 : float<inch> = 1.0 > let length3 = length1 + length2;; let length3 = length1 + length2;; ------------------------^^^^^^^ stdin(41,25): error FS0001: The unit of measure 'inch' does not match the unit of measure 'cm' > let length3 = length1 + length2 * cmPerInch;; val length3 : float<cm> = 3.54
No comments:
Post a Comment