最终的返回值是如何构造的……
[Msg | important()]第一次返回时,确定最终返回值的形式。唯一需要担心的是,我们还不知道最终返回值的所有细节。因此[Msg | important()] 中的important() 将继续被评估。下面举例说明最终返回值[high,high,low,low]是如何构造的。
[high | important( )] <---- Defines the final form
---------------------------------
[high | important( )] <---- Adds more details
------------------------
normal( ) <---- Adds more details
------------------------
[low | normal( )] <---- Adds more details
----------------
[low | normal()] <---- Adds more details
--------
[ ] <---- Adds more details
------------------------------------------
[high | [high | [low | [low | []]]]]
[high,high,low,low] <---- The final return value
代码的工作原理...
在函数important/0,after 0简单的意思是“我不等消息来”——如果我的邮箱里有消息,我会看;如果没有,我会继续(执行normal())而不是在那里等待。邮箱里已经有{15, high}, {7, low}, {1, low}, {17, high}。在 Erlang 中,邮箱中的消息不以先到先得的顺序排队。 receive 子句可能很挑剔。它扫描邮箱中的所有邮件并“挑选”它想要的邮件。在我们的例子中,根据{Priority, Msg} when Priority > 10,{15, high} 和 {17, high} 首先被选中。
之后,函数normal/0 接管。 {7, low}, {1, low} 按顺序处理(consed)。最后,我们得到了[high,high,low,low]。
显示处理顺序的修改版本...
我们可以稍微修改一下代码,让处理(consing)的顺序更加明确:
-module(prior).
-compile(export_all).
important() ->
receive
{Priority, Msg} when Priority > 10 ->
[{Priority, Msg} | important()] % <---- Edited
after 0 ->
normal()
end.
normal() ->
receive
{Priority, Msg} -> % <---- Edited
[{Priority, Msg} | normal()] % <---- Edited
after 0 ->
[]
end.
在 shell 中运行它:
4> c(prior).
{ok, prior}
5> self() ! {15, high}, self() ! {7, low}, self() ! {1, low}, self() ! {17, high}.
{17,high}
6> prior:important().
[{15,high},{17,high},{7,low},{1,low}]