module - Erlang 中的参数化模块

标签 module erlang parameterized

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

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

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

我搜索了谷歌,发现了一篇关于参数化模块的论文:链接太大了,但我确信如果你关注here,你会得到这篇论文。

这些模块不能直接调用,而是通过它们自身的实例来调用。它使模块的行为就像它们很有趣一样。我开始意识到这是运行时系统中的一个非官方功能。让我困惑的是 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,请求)== mochiweb_request。很有趣!?!!!?

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

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

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

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

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

最佳答案

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

它在 R16B 中被删除。来自 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

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

参数化模块的使用已removed from Mochiweb ,从版本 2.4.0 开始,尽管对以前的参数化模块的调用看起来仍然相同,因为保留了参数化模块(元组模块)的实现机制以实现向后兼容性。尽管对元组调用的支持已被保留从 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让这种类型的代码继续工作。

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

来自Technical Board decision announcing the end of parameterised modules日期为 2012 年 10 月 16 日:

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.

关于module - Erlang 中的参数化模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5339012/

相关文章:

python - HTTP 错误 400 错误请求 python

iis-7 - IIS7 将 web.config 从经典迁移到集成问题

import - 谷歌 Colab : Reload imported modules

forms - 使用包含的头文件编译表单

ubuntu - 在 native 模式下运行 erlang 代码

erlang - 如何使用 erlang(如果可能,没有库)做一个简单的 http post/get with header?

c# - 如果我通过ParameterizedThreadStart将对象传递给线程,我以后可以访问它吗?

java - 如何使用参数化数据的多个来源

python - 从不同的 .py 文件调用函数时找不到 Errno 2 文件