Erlang演员如何与OOP对象不同? [英] How do Erlang actors differ from OOP objects?

查看:162
本文介绍了Erlang演员如何与OOP对象不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个这样定义的Erlang演员:

  counter(Num) - > 
收到
{From,increment} - >
从! {self(),new_value,Num + 1}
计数器(Num + 1);
结束。

同样,我有一个这样定义的Ruby类:

  class Counter 
def initialize(num)
@num = num
end

def increment
@num + = 1
end
end

Erlang代码以函数式写成,使用尾递归来维持状态。但是,这种差异有什么意义的影响?对于我天真的眼睛,这两个东西的界面看起来是一样的:你发送消息,状态被更新,并且你回到新状态的表示。



<功能编程经常被描述为与OOP完全不同的范例。但是Erlang演员似乎正在做什么对象应该做的:维护状态,封装和提供基于消息的界面。



换句话说,当我在Erlang演员之间传递消息,与Ruby对象之间传递消息的方式有什么不同?



我怀疑功能/ OOP二分法的后果比我看到了有人可以指出他们吗?



我们放弃一下这个事实,即Erlang演员将由虚拟机安排,因此可以与其他代码同时运行。我意识到这是Erlang和Ruby版本之间的重大区别,但这不是我所得到的。并发可能在其他语言,包括Ruby。虽然Erlang的并发可能会有很大的不同(有时更好),但我并没有真正询问性能差异。



相反,我对功能 - vs-OOP方面的问题。

解决方案


换句话说,当我传递消息之间Erlang演员,与Ruby对象之间传递消息的方式有何不同?


区别在于Ruby没有消息传递,而是在同一个线程中执行的方法调用,如果您有多线程应用程序,则可能会导致同步问题。所有线程都可以访问彼此的线程内存。



在Erlang中,所有actors都是独立的,而改变另一个actor状态的唯一方法是发送消息。没有进程可以访问任何其他进程的内部状态。


Suppose I have an Erlang actor defined like this:

counter(Num) ->
  receive
    {From, increment} ->
      From ! {self(), new_value, Num + 1}
      counter(Num + 1);
  end.    

And similarly, I have a Ruby class defined like this:

class Counter
  def initialize(num)
    @num = num
  end

  def increment
    @num += 1
  end
end

The Erlang code is written in a functional style, using tail recursion to maintain state. However, what is the meaningful impact of this difference? To my naive eyes, the interfaces to these two things seem much the same: You send a message, the state gets updated, and you get back a representation of the new state.

Functional programming is so often described as being a totally different paradigm than OOP. But the Erlang actor seems to do exactly what objects are supposed to do: Maintain state, encapsulate, and provide a message-based interface.

In other words, when I am passing messages between Erlang actors, how is it different than when I'm passing messages between Ruby objects?

I suspect there are bigger consequences to the functional/OOP dichotomy than I'm seeing. Can anyone point them out?

Let's put aside the fact that the Erlang actor will be scheduled by the VM and thus may run concurrently with other code. I realize that this is a major difference between the Erlang and Ruby versions, but that's not what I'm getting at. Concurrency is possible in other languages, including Ruby. And while Erlang's concurrency may perform very differently (sometimes better), I'm not really asking about the performance differences.

Rather, I'm more interested in the functional-vs-OOP side of the question.

解决方案

In other words, when I am passing messages between Erlang actors, how is it different than when I'm passing messages between Ruby objects?

The difference is that in traditional languages like Ruby there is no message passing but method call that is executed in the same thread and this may lead to synchronization problems if you have multithreaded application. All threads have access to each other thread memory.

In Erlang all actors are independent and the only way to change state of another actor is to send message. No process have access to internal state of any other process.

这篇关于Erlang演员如何与OOP对象不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆