Erlang 中的参数化模块

发布于 2024-10-22 04:33:33 字数 1884 浏览 2 评论 0 原文

我正在浏览 mochiweb 源代码,并看到一些我以前从未使用过的东西。模块声明,特别是在 mochiweb http 库中找到的 mochiweb_requestmochiweb_response 模块中。以下是该模块的开始方式:

-module(mochiweb_request,[Socket, Method, RawPath, Version, Headers]).
-author(...).

然后在模块中您会看到 get(socket) -> Socket;get(方法)->方法; ....
这让我很困惑。当我尝试获取其中一个此类模块的模块信息时,编译器在返回中添加了一些内容:{abstract,true}
mochiweb_request:module_info()。。事实上,他们的文档将这些模块称为“抽象模块”。

我搜索了谷歌,发现了一篇关于参数化模块的论文:链接太大了,但我相信如果你按照 这里

这些模块不能直接调用而是调用通过他们自己的实例。它使模块的行为就像它们很有趣一样。我开始意识到这是运行时系统中的一个非官方功能。让我困惑的是 mochiweb 的人用得很好!在 mochiweb 模块中,您会发现自己编写:

loop(Req,_DocRoot)->
    "/" ++ Path = Req:get_path(),
    Body = Req:recv_body(),
    Method = Req:get(method),
    ...,
    ....,
    Response = Req:ok({"text.html;charset=utf-8",[],chunked}),
    Response:write_chunk("Some text here....."),
    ...

尝试 io:format("\n\t Req = ~p~n",[Req]) 揭示了一个复杂的数据结构(一个元组) ),其中 element(1,Req) == mochiweb_request。很有趣!?!!!?

问题 1 是:目前在生产中使用是否稳定,或者我可以等到它正式发布吗?

问题 2 是:如果尚未正式发布,mochiweb 人员如何获得使用它的信心?

问题3:为什么还没有正式发布? (因为,对我来说,它带来了一些面向对象的功能)

问题4:请问有人用过吗?他/她在哪些情况下使用了这些参数化模块?为什么?您能否指出我们在那里查看或发布一些源代码的链接,以便我们可以了解有关此功能的更多信息?

最后一个问题:我在 Erlang 文档中没有找到任何谈到此功能的地方。没有教科书,甚至没有。那么那些已经使用过它的人是如何知道如何以及为什么使用它的呢?它是否已包含在 此处 的 Erlang 运行时系统的商业版本中?

I was going through mochiweb source code and got to see something i never used before. The module declaration especially in mochiweb_request and mochiweb_response modules found in the mochiweb http library. Here is how the module begins:

-module(mochiweb_request,[Socket, Method, RawPath, Version, Headers]).
-author(...).

Then in the module you see get(socket) -> Socket;get(method)-> Method; ....
This has confused me. When i tried getting the module info of one of these such module, the compiler had added something: {abstract,true} in the return to:
mochiweb_request:module_info().. Infact, their documentation refers to these modules as abstract modules.

I have searched google and found a paper on parameterised modules: the link is so big but am sure you will get the paper if u follow on here

These modules cannot be called directly but are called through instances of them selves. It makes modules behave as though they were funs. I have come to realise that its an unofficial feature in the runtime system. What confuses me is that the mochiweb guys are using it well!. In a mochiweb module, you will find your self writing:

loop(Req,_DocRoot)->
    "/" ++ Path = Req:get_path(),
    Body = Req:recv_body(),
    Method = Req:get(method),
    ...,
    ....,
    Response = Req:ok({"text.html;charset=utf-8",[],chunked}),
    Response:write_chunk("Some text here....."),
    ...

Trying to io:format("\n\t Req = ~p~n",[Req]) reveales a complex data structure (a tuple) whose element(1,Req) == mochiweb_request. It is interesting!?!!!?

Question 1 is: Is it stable to use in production for now or i can wait till it is made official?

Question 2 is: How did the mochiweb guys get the confidence of using it, if it is not yet official?

Question 3: Why is it not yet official? (because, to me, it brings some Object Oriented features in)

Question 4: Is there anyone out there who has used it also? In which cases has he/she used these parameterised modules? Why? Can you point us there to see or post a link to some source code so we can find out more on this feature?

Last Question: No where in the Erlang Docs have i found this feature talked about. No text book, Not even home. So how did those who have used it already find out how and why to use it? Has it already been included in the commercial version of the Erlang Run time system found here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冰魂雪魄 2024-10-29 04:33:33

问题1是:目前在生产中使用是否稳定,或者我可以等到它正式发布吗?

它在 R16B 中被删除。来自自述文件

OTP-10616 实验功能“参数化模块”(也称为
“抽象模块”)已被删除。对于应用程序
依赖于参数化模块,有一个解析转换
仍可用于使用参数化模块。这
解析变换可以在以下位置找到:
http://github.com/erlang/pmod_transform

问题 2 是:mochiweb 人员是如何获得如果尚未正式发布,您是否有信心使用它?

不过,从 2.4.0 版本开始,参数化模块的使用已从 Mochiweb 中删除对以前的参数化模块的调用看起来仍然相同,因为保留参数化模块(元组模块)的实现机制是为了向后兼容。即使在 Erlang/OTP 21.0

OTP-14497 应用程序:编译器、erts

* 潜在的不兼容 *

对“元组调用”的支持已从
运行时系统。元组调用是一种无证且
允许模块参数的不受支持的功能
对于元组的应用操作:Var = dict:new(), Var:size()。这个“功能”经常引起混乱,
尤其是当此类调用失败时。堆栈跟踪将
指出源代码中不存在的函数
代码。

对于需要使用参数化模块的遗留代码
或由于其他原因元组调用,有一个新的
编译器选项称为tuple_calls。当该选项为
给定,编译器将生成额外的代码
模拟模块所在位置的调用的旧行为
一个变量。

Mochiweb 现在对此类类型使用 tuple_calls 编译器选项代码以继续工作。

问题3:为什么还没有正式发布? (因为,对我来说,它带来了一些面向对象的功能)

来自 技术委员会决定宣布参数化模块结束,日期为 2012 年 10 月 16 日:

董事会承认许多软件都依赖于此功能,尽管它一直处于实验阶段。当前的实现形式是不可接受的,并且参数化模块本身从未被接受为该语言的功能。该功能也不兼容 module-fun 等,并且未与 OTP 中的其他工具完全集成。

Question 1 is: Is it stable to use in production for now or i can wait till it is made official?

It was removed in R16B. From the README:

OTP-10616 The experimental feature "parameterized modules" (also called
"abstract modules") has been removed. For applications that
depends on parameterized modules, there is a parse transform
that can be used to still use parameterized modules. The
parse transform can be found at:
http://github.com/erlang/pmod_transform

Question 2 is: How did the mochiweb guys get the confidence of using it, if it is not yet official?

Use of parameterised modules has been removed from Mochiweb, starting from release 2.4.0, though calls to formerly parameterised modules still look the same, as the implementation mechanism for parameterised modules (tuple modules) is kept for backwards compatibility. even though support for tuple calls were removed from the compiler in Erlang/OTP 21.0:

OTP-14497 Application(s): compiler, erts

* POTENTIAL INCOMPATIBILITY *

Support for "tuple calls" have been removed from the
run-time system. Tuple calls was an undocumented and
unsupported feature which allowed the module argument
for an apply operation to be a tuple: Var = dict:new(), Var:size(). This "feature" frequently caused confusion,
especially when such call failed. The stacktrace would
point out functions that don't exist in the source
code.

For legacy code that need to use parameterized modules
or tuple calls for some other reason, there is a new
compiler option called tuple_calls. When this option is
given, the compiler will generate extra code that
emulates the old behavior for calls where the module is
a variable.

Mochiweb now uses the tuple_calls compiler option for this type of code to keep working.

Question 3: Why is it not yet official? (because, to me, it brings some Object Oriented features in)

From the Technical Board decision announcing the end of parameterised modules dated 16 Oct 2012:

The board acknowledges that a lot of software relies on this feature although it's always been experimental. The current form of the implementation is not acceptable and the parameterized modules as such has never been accepted as a feature in the language. The feature is also not compatible with for example module-fun's and is not fully integrated with the rest of the tools in OTP.

顾忌 2024-10-29 04:33:33

问题1是:目前在生产中使用是否稳定,或者我可以等到它正式发布吗?

它对于生产使用非常稳定,并且已经存在了一段时间了。它不是官方标准的一部分。

问题 2 是:如果尚未正式发布,mochiweb 人员如何获得使用它的信心?

你必须向 mochiweb 的人询问这个问题。也许他们相信,如果它被拉动,他们可以很快改变它。

问题3:为什么还没有正式发布? (因为,对我来说,它带来了一些面向对象的功能)

因为它充满了争议。目前还不清楚它给语言带来了什么好处以及它如何使事情变得更容易,因此 P.Modules 有它的支持者和反对者。因此,目前的观点是它是实现的一部分,因此人们可以使用它,看看他们是否觉得它使他们的代码更易于阅读和编写。非官方性意味着它可以在不被弃用的情况下被拉动,而且似乎 Erlang 人保留了这一权利。

个人偏见:我有点喜欢它,但我永远不会使用它来将 OOP 功能引入 Erlang。 OOP 是一个丑陋的垃圾庞然大物,在编程中永远没有地位。只是痛苦会困扰你的程序,直到它们彻底腐烂,像僵尸一样走来走去,发疯。此时唯一的解决办法就是霰弹枪。相反,我想将它用作 ML 风格的函子——它更加静态,因为我觉得它更符合 Erlang 的习惯用法。

最后一个问题:我在 Erlang 文档中没有找到任何谈到此功能的地方。没有课本,甚至没有家。那么那些已经使用过它的人是如何知道如何以及为什么使用它的呢?它是否已包含在此处找到的 Erlang 运行时系统的商业版本中?

作者在几年前的 Erlang 会议上介绍了这个东西。此后一直是口碑相传等相结合的方式。

Question 1 is: Is it stable to use in production for now or i can wait till it is made official?

It is very stable for production use and has been for some time now. It is not part of the official standard.

Question 2 is: How did the mochiweb guys get the confidence of using it, if it is not yet official?

You will have to ask the mochiweb guys for this. Perhaps they believe they can quickly change it if it was pulled.

Question 3: Why is it not yet official? (because, to me, it brings some Object Oriented features in)

Because it is littered with controversy. It is not clear what benefits it bring to the language and how it makes stuff easier to do, so P. Modules have its proponents and opponents. Hence, the current standpoint is that it is part of the implementation, so people can play with it and see if they feel it makes their code easier to read and write. The non-officiality means it can be pulled without deprecation though, and it seems as if the Erlang guys reserve that right.

personal bias: I kind-of like it, but I won't ever be using it for getting OOP-features into Erlang. OOP is an ugly behemoth of utter crap that has no place in programming ever. It is just misery that will haunt your programs until they are rotten to the core, walking around like zombies and being mad. The only solution at that point is the shotgun. Rather, I'd like to use it as ML-style functors - which is more static in I feel it matches the idioms of Erlang better.

Last Question: No where in the Erlang Docs have i found this feature talked about. No text book, Not even home. So how did those who have used it already find out how and why to use it? Has it already been included in the commercial version of the Erlang Run time system found here?

The author presented the thing at an Erlang conference some years ago. Since then it has been a combination of word-of-mouth and so on.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文