Erlang有没有办法? [英] Does Erlang have methods?

查看:211
本文介绍了Erlang有没有办法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

E.g。有这样的东西:

  O = widget:new(),
O:whirl()

我似乎记得看到一些这样的代码(也许我是想像的),但我没有看到在我读过的教程中从我所看到的,最接近的是:

  O = widget:new(),
widget这个不错,但不必重复

解决方案

不,Erlang没有方法。 Erlang具有进程,而不是对象,并通过发送消息来与他们进行通信,而不是调用方法。而已。这就是它所有的一切。



在Erlang中最接近新的,意味着它在Java或c ++是 spawn 。 (参数化模块讨论涉及到与C ++类型语言相似的东西,其中 new 保留内存,调用构造函数等)。



实际上有两个方面:数据对象(如一个dict或list或某些东西)和进程(使用 generatewn创建的东西)。



中,您可能会看到类似于

  SomeDict = dict:new(),

  OtherDict = dict:from_list(KV_List)

这确实创造了一些东西,但它不是Java或C ++意义上的对象,它是术语中的老)中的对象,它是一个命名的引用在记忆中。实际上,你与上面所示的方式进行交互:

  D = dict:new(),
ok = some_operation(D),

其中 some_operation / 1 可能是任何东西,无论是 dict:foo()还是别的东西。冒号之前的部分是一个模块标识符,告诉运行时你要调用的函数的命名空间是什么 - 没有更多的。



另一件事, spawn ,更像C ++中的,您希望创建一个活着的,拥有胳膊和腿的完整的东西 - - 一个可以做的事情的名词:

  Pid = spawn(Mod,Fun,Args),
Pid! {some,message},

大多数时候你只看到 erlang在原型或非OTP代码中,发送/ 2 函数(也写为中缀运算符)。通常,这是通过接口函数隐藏的,它吸收了您一直发送异步消息与您的进程通信的事实。



对于一些更深入的说明,我建议阅读了解一些Erlang - 作者深入解释了这一点和其他基本概念。



无论你做什么,不要陷入愚蠢的陷阱,Erlang是Java。这只是一个伟大的方式来跳过你自己的先入之见,感到沮丧。这可能是我看到人们做的第一个初学者错误...


E.g. is there something like this:

O = widget:new(),
O:whirl()

I seem to recall seeing some code like this (maybe I was imagining it), but I haven't seen it in the tutorials that I've read. From what I've seen, the closest thing is this:

O = widget:new(),
widget:whirl(O)

That's not too bad, but not having to repeat widget: in the second expression would be nice.

解决方案

No, Erlang does not have methods. Erlang has processes, not objects, and you communicate with them by messaging them, not calling methods on them. That's it. That's all there is to it.

The closest thing to new in Erlang that means what it means in Java or C++ is spawn. (The parameterized module discussion touches on something very different from what you would expect coming from a C++ type language where new reserves memory, calls a constructor, etc.)

There are actually two aspects to this: data objects (like a dict or list or something) and processes (things you create with spawn).

Within a function definition you might see something like

SomeDict = dict:new(),

or

OtherDict = dict:from_list(KV_List)

This does indeed create something, but its not an "object" in the Java or C++ sense, it is an "object" in the (older) sense of the term that it is a named reference to something in memory. And indeed you interact with it the same way you demonstrated above:

D = dict:new(),
ok = some_operation(D),

where some_operation/1 might be anything, whether it is dict:foo() or something else. The part before the colon is a module identifier, telling the runtime what namespace that function you're calling exists in -- nothing more.

The other thing, spawn, is much more like new in C++, where you want to create a complete thing that is alive and has arms and legs -- a noun that can do verby things:

Pid = spawn(Mod, Fun, Args),
Pid ! {some, message},

Most of the time you only see the erlang:send/2 function (also written as the infix ! operator) in prototype or non-OTP code. Usually this is hidden from you by interface functions that abstract away the fact that you are sending async messages all the time to communicate with your processes.

For some more in-depth explanation, I recommend reading Learn You Some Erlang -- the author explains this and other basic concepts in some depth.

Whatever you do, do not fall into the simpleton's trap of thinking Erlang is Java. That is just a great way to trip over your own preconceptions and get frustrated. This is probably the #1 beginner mistake I see people make...

这篇关于Erlang有没有办法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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