2
我有一个F#3.0剂包裹在一个类:F#奇怪剂重新初始化行为
type AgentWrapper() =
let myAgent = Agent.Start(fun inbox ->
let rec loop (state: int) =
async {
let! (replyChannel: AsyncReplyChannel<int>) = inbox.Receive()
let newState = state + 1
replyChannel.Reply newState
return! loop newState
}
loop 0)
member private this.agent = myAgent
member this.Send() =
this.agent.PostAndReply (fun replyChannel -> replyChannel)
当我将消息发送到它,如下所示:
let f = new AgentWrapper()
f.Send() |> printf "Reply: %d\n"
f.Send() |> printf "Reply: %d\n"
f.Send() |> printf "Reply: %d\n"
我得到预期的回应:
Reply: 1
Reply: 2
Reply: 3
但是,如果我删除让绑定的剂并将其直接分配给this.agent属性:
type AgentWrapper() =
member private this.agent = Agent.Start(fun inbox ->
let rec loop (state: int) =
async {
let! (replyChannel: AsyncReplyChannel<int>) = inbox.Receive()
let newState = state + 1
replyChannel.Reply newState
return! loop newState
}
loop 0)
member this.Send() =
this.agent.PostAndReply (fun replyChannel -> replyChannel)
然后我得到的答复:
Reply: 1
Reply: 1
Reply: 1
我已经在这盯着几个小时,我不明白为什么代理我每次打电话给AgentWrapper.Send都会重新初始化。每次我打电话时,感觉就像this.agent被重新分配(即像一个方法,而不是一个属性)。我错过了什么?
卫生署!我今天跑在“愚蠢的”......谢谢。 – Akash 2013-02-26 11:27:50