我正在尝试制作一个正在处理订单的酒吧模拟器。发送给代理的消息属于这种类型。
type DTGCafeMessage =
| OrderDrink of Drink * float
| LeaveAComment of string
代理是下面实现的一个栏类。
type Bar() =
let dtgCafeAgent =
MailboxProcessor.Start
(fun inbox ->
let rec messageLoop () =
async {
let! msg = inbox.Receive()
match msg with
| OrderDrink (drink, amount) ->
let drinkPrice : float = getPrice drink
let mutable totalPrice = ((drinkPrice: float) * (amount: float))
if drink.type' = Coffee then
totalPrice <- dgtVAT totalPrice VAT
printfn
"Please pay DKK%d for your %d %A %A drinks. %s!"
(Convert.ToInt32(totalPrice))
(Convert.ToInt32(amount))
(string drink.type')
(string drink.size)
"Thanks!"
| LeaveAComment (comment) -> printf "Comment: %A" comment
return! messageLoop ()
}
messageLoop ())
member this.Order msg = dtgCafeAgent.Post msg
当我将消息发送给代理时,它正在以非常混乱的方式打印内容。
let bar = Bar()
let testDrink = { type' = Coffee; size = Small }
bar.Order(OrderDrink({ type' = Coffee; size = Small }, 2.0))
let orderDrinks =
[ (OrderDrink({ type' = Coffee; size = Small }, 1.0))
(OrderDrink({ type' = Coffee; size = Medium }, 2.0))
(OrderDrink({ type' = Juice; size = Small }, 3.0))
(OrderDrink({ type' = Soda; size = Medium }, 4.0))
(OrderDrink({ type' = Milk; size = Large }, 5.0)) ]
orderDrinks |> List.map (fun o -> bar.Order o)
// output
> orderDrinks |> List.map (fun o -> bar.Order o);;
valPlease pay DKK24 for your 1 "Coffee" "Small" drinks. Thanks!!
it Please pay DKK72 for your 2 "Coffee" :"Medium" drinks. Thanks!!
unitPlease pay DKK 45 for your 3 list"Juice" "Small" drinks. Thanks!!
=Please pay DKK51 for your 4 "Soda" "Medium" drinks. Thanks!!
[()Please pay DKK;125 for your 5 "Milk" "Large" drinks. Thanks!!
(); (); (); ()]
如我所见,它应该打印代码中编写的每条语句。