埃尔朗期货与承诺 [英] Futures and Promises in Erlang
问题描述
Erlang是否具有未来与承诺的等同内容?或者由于未来和承诺正在解决Erlang系统中不存在的问题(例如同步编排),因此我们不需要Erlang。
Does Erlang has equivalents for Future and Promises? Or since future and promises are solving a problem that doesn't exist in Erlang systems (synchronisation orchestrating for example), and hence we don't need them in Erlang.
如果我想要Erlang的期货和承诺的语义,可以通过Erlang流程/行为者来模拟?
If I want the semantics of futures and promises in Erlang, they could be emulated via Erlang processes/actors?
推荐答案
您可以轻松实现Erlang的未来:
You could easily implement a future in Erlang like this:
F = fun() -> fancy_function() end,
% fancy code
Pid = self(),
Other = spawn(fun() -> X = F(), Pid ! {future, self(), X} end).
% more fancy code
Value = receive {future, Other, Val} -> Val end.
在模块中建立这个功能并从中构建链接也很简单,但是说实话从来没有想过这样的东西。如果您只是自由地发送消息,你会更灵活。
Having this functionality in a module and building chains from it should be easy too, but to be honest I never actually missed something like this. You are more flexible if you just freely send messages around.
这篇关于埃尔朗期货与承诺的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!