适合C# Actor的消息执行方式(2):C# Actor的尴尬
2010-09-30 20:50:29 来源:WEB开发网首先还是定义PingMsg和PongMsg:
type PingMsg =
| Ping of PongMsg Actor
| Finished
and PongMsg =
| Pong of PingMsg Actor
这里体现了F#类型系统中的Discriminated Unions。简单地说,它的作用是把一种类型定义为多种表现形式,这个特性在Haskell等编程语言中非常常见。Discriminated Unions非常适合模式匹配,现在的ping对象和pong对象便可定义如下(在这里还是使用了ActorLite,而不是F#标准库中的MailboxProcessor来实现Actor模型):
let (<<) (a:_ Actor) msg = a.Post msg
let ping =
let count = ref 5
{ new PongMsg Actor() with
override self.Receive(message) =
match message with
| Pong(pong) ->
printfn "Ping received pong"
count := !count - 1
if (!count > 0) then
pong << Ping(self)
else
pong << Finished
self.Exit() }
let pong =
{ new PingMsg Actor() with
override self.Receive(message) =
match message with
| Ping(ping) ->
printfn "Pong received ping"
ping << Pong(self)
| Finished ->
printf "Fininshed"
self.Exit() }
- ››适合做商品团购营销的网站
- ››适合所有浏览器hack的CSS技巧
- ››消息称中国移动即将获得iPhone 4销售权
- ››适合C# Actor的消息执行方式(1):Erlang中的模式...
- ››适合C# Actor的消息执行方式(2):C# Actor的尴尬...
- ››适合C# Actor的消息执行方式(3):中看不中用的解...
- ››适合C# Actor的消息执行方式(4):阶段性总结
- ››适合C# Actor的消息执行方式(5):一个简单的网络...
- ››适合C# Actor的消息执行方式(6):协变与逆变
- ››适合1-5个月经验的seo优化全过程分享
- ››消息称联通高层赴美谈引入iPhone4 将带WiFi
- ››消息称微软将在近期发布IE9 beta
更多精彩
赞助商链接