Thursday, November 13, 2008

F# MailboxProcessor

The Mailboxprocessor class has changed slightly in Version 1.9.6.2
You have to modify the example 13.11 from the Expert F# Book as following to run

#light
// ----------------------------
// Listing 13-11.
/// The internal type of messages for the agent
type internal msg = Increment of int Fetch of AsyncReplyChannel Stop
type CountingAgent() =
let counter = MailboxProcessor.Start(fun inbox ->
// The states of the message-processing state machine...
let rec loop(n) =
async { let! msg = inbox.Receive()
match msg with
Increment m ->
// increment and continue.
return! loop(n+m)
Stop ->
// exit
return ()
Fetch replyChannel ->
// post response to reply channel and continue
do replyChannel.Reply(n)
return! loop(n) }
// The initial state of the message-processing state machine...
loop(0))
member a.Increment(n) = counter.Post(Increment(n))
member a.Stop() = counter.Post(Stop)
member a.Fetch() = counter.PostAndReply(fun replyChannel -> Fetch(replyChannel))
// ----------------------------
let counter = new CountingAgent()
counter.Increment(1)
let v=counter.Fetch()
printfn "%d" v
counter.Increment(2)
let s=counter.Fetch()
printfn "%d" s
counter.Stop()