module UnitsOfMeasure // The previous syntax defines unit-name as a unit of measure. The optional part is used to define a new measure in terms of previously defined units. For example, the following line defines the measure cm (centimeter).+ // Distance, cm [] type cm // The following line defines the measure ml (milliliter) as a cubic centimeter (cm^3). // Volume, milliliters. [] type ml = cm^3 // Mass, grams. [] type g // Mass, kilograms. [] type kg // Weight, pounds. [] type lb // Distance, meters. [] type m // Distance, inches. [] type inch // Distance, feet [] type ft // Time, seconds. [] type s // Force, Newtons. [] type N = kg m / s // Pressure, bar. [] type bar // Pressure, Pascals [] type Pa = N / m^2 // Volume, liters. [] type L // Define conversion constants. let gramsPerKilogram : float = 1000.0 let cmPerMeter : float = 100.0 let cmPerInch : float = 2.54 let mlPerCubicCentimeter : float = 1.0 let mlPerLiter : float = 1000.0 // Define conversion functions. let convertGramsToKilograms (x : float) = x / gramsPerKilogram let convertCentimetersToInches (x : float) = x / cmPerInch // Using Generic Units let genericSumUnits ( x : float<'u>) (y: float<'u>) = x + y let v1 = 3.1 let v2 = 2.7 let x1 = 1.2 let t1 = 1.0 // OK: a function that has unit consistency checking. let result1 = genericSumUnits v1 v2 // Error reported: mismatched units. // Uncomment to see error. // let result2 = genericSumUnits v1 x1 // Creating Aggregate Types with Generic Units // Define a vector together with a measure type parameter. // Note the attribute applied to the type parameter. type vector3D<[] 'u> = { x : float<'u>; y : float<'u>; z : float<'u>} // Create instances that have two different measures. // Create a position vector. let xvec : vector3D = { x = 0.0; y = 0.0; z = 0.0 } // Create a velocity vector. let v1vec : vector3D = { x = 1.0; y = -1.0; z = 0.0 }