技术开发 频道

C#Actor消息执行:C# Actor的尴尬

 【IT168技术文档】

转载自Jeff Zhao的博客

    在上一篇文章中,我们简单解读了Erlang在执行消息时候的方式。而现在,我们就一起来看看,C# Actor究竟出现了什么样的尴尬。此外,我还打算用F#进行补充说明,最终我们会发现,虽然F#看上去很美,但是在实际使用过程中依旧有些遗憾。

    Erlang中的Tag Message

    老赵在上一篇文章里提到,Erlang中有一个“约定俗成”,使用“原子(atom)”来表示这条消息“做什么”,并使用“绑定(binding)”来获取做事情所需要的“参数”。Erlang大拿,《Programming Erlang》一书的主要译者jackyz同学看了老赵的文章后指出,这一点在Erlang编程规范中有着明确的说法,是为“Tag Message”:

5.7 Tag messages

All messages should be tagged. This makes the order in the receive statement less important and the implementation of new messages easier.

Don’t program like this:

loop(State) ->
receive
...
{Mod, Funcs, Args} -> % Don't do this
apply(Mod, Funcs, Args},
loop(State);
...
end.
The new message {get_status_info, From, Option} will introduce a conflict if it is placed below the {Mod, Func, Args} message.

If messages are synchronous, the return message should be tagged with a new atom, describing the returned message. Example: if the incoming message is tagged get_status_info, the returned message could be tagged status_info. One reason for choosing different tags is to make debugging easier.

This is a good solution:

loop(State) ->
receive
...
{execute, Mod, Funcs, Args} -> % Use a tagged message.
apply(Mod, Funcs, Args},
loop(State);
{get_status_info, From, Option} ->
From ! {status_info, get_status_info(Option, State)},
loop(State);
...
end.
 

  第一段代码使用的模式为拥有三个“绑定”的“元组”。由于Erlang的弱类型特性,任何拥有三个元素的元组都会被匹配到,这不是一个优秀的实践。在第二个示例中,每个模式使用一个“原子”来进行约束,这样可以获取到相对具体的消息。为什么说“相对”?还是因为Erlang的弱类型特性,Erlang无法对From和Option提出更多的描述。同样它也无法得知execute或get_status_info这两个tag的来源——当然,在许多时候,它也不需要关心是谁发送给它的。

0
相关文章