This project is read-only.
Define units of measure

Category:
Units of Measure
Description: Define and use measure unit. The units of measure is a compile time feature, so it won't go into the binary file.
Code:
[<Measure>]type litre
[<Measure>]type pint

let MeasureSample1() =
let v1 = 2.
let v2 = 1.
let ratio = 1.0 / 1.76056338
let pintToLitre pints =
pints * ratio
let newVol = v1 + (pintToLitre v2)
printfn "converted value is %A" newVol

Execution Result:
converted value is 2.568

convert units of measure to float

Category:
Units of Measure
Description: convert measure unit to float.
Code:
[<Measure>]type litre
[<Measure>]type pint

let MeasureSample2() =
let a = 1.
let (b:float) = float(a)
printfn "measure unit %A" a
printfn "float value %f" b

Execution Result:
measure unit 1.0
float value 1.000000

convert units of measure and LanguagePrimitives

Category:
Units of Measure
Description: convert measure and LanguagePrimitives. The conversion ratio is defined and the conversion is based on the ratio.
Code:
[<Measure>]type litre
[<Measure>]type pint

let MeasureSample3() =
let l:float = LanguagePrimitives.FloatWithMeasure 10.
let ratio = 1.0 / 1.76056338
let pints = l / ratio
printfn "%A pints = %A liter" pints l

Execution Result:
17.6056338 pints = 10.0 liter

Aggreate type and LanguagePrimitives

Category:
Units of Measure
Description: Aggreate type and LanguagePrimitives.
Code:
type AccountState = 
| Overdrawn
| Silver
| Gold

[<Measure>] type USD
[<Measure>] type CND

type Account<[[<Measure>]] 'u>() =
let mutable balance = 0.0<_>
member this.State
with get() =
match balance with
| _ when balance <= 0.0<_> -> Overdrawn
| _ when balance > 0.0<_> && balance < 10000.0<_> -> Silver
| _ -> Gold
member this.PayInterest() =
let interest =
match this.State with
| Overdrawn -> 0.
| Silver -> 0.01
| Gold -> 0.02
interest * balance
member this.Deposit(x:float<_>) = balance <- balance + x
member this.Withdraw(x:float<_>) = balance <- balance - x

let MeasureSample4() =
let account = Account()
account.Deposit(LanguagePrimitives.FloatWithMeasure 10000.)
printfn "us interest = %A" (account.PayInterest())
account.Withdraw(LanguagePrimitives.FloatWithMeasure 20000.)
printfn "us interest = %A" (account.PayInterest())

let canadaAccount = Account()
canadaAccount.Deposit(LanguagePrimitives.FloatWithMeasure 10000.)
canadaAccount.Withdraw(LanguagePrimitives.FloatWithMeasure 500.)
printfn "canadian interest = %A" (canadaAccount.PayInterest())

Execution Result:
us interest = 200.0
us interest = 0.0
canadian interest = 95.0

Aggreate type and LanguagePrimitives

Category:
Units of Measure
Description: Aggreate type and LanguagePrimitives. The LanguagePrimitives is used to make a number (e.g. float) into units of measure.
Code:
[<Measure>]type JPY
[<Measure>]type CNY
type BankAccount<[<Measure>]'u>() =
let mutable balance : float<'u> = 0.0<_>
member acct.HasLotsOfMoney = balance > (LanguagePrimitives.FloatWithMeasure 10000.0) //10000 units
member acct.Deposit(amt) = balance <- balance + amt
member acct.Withdraw(amt) = balance <- balance - amt

let MeasureSample5() =
let a = BankAccount()
a.Deposit(10.)

let b = BankAccount()
b.Deposit(10.)

printfn "show how to deposit right currency to right account"

Execution Result:
show how to deposit right currency to right account

Last edited Sep 13, 2011 at 1:04 AM by ttliu2000, version 2

Comments

No comments yet.