ITeye博客

把Module搞得像Class

gigix · 2007-11-09 22:59 · 企业应用建筑师 · 编程语言 · C · C++ · C# · Erlang · Markdown

http://www.clickcaster.com/items/the-black-art-of-erlangs-parameterized-modules

To declare a parameterized module, simply specify some variable names in the module declaration...Parameterized modules give you what are effectively constants which can be declared per "instance" of a module.

非标准用法,风险自负。

评论(1)

  1. #1 楼 · Trustno1 · 2007-11-10 07:54

    这个提案实际上是一个不那么甜也不那么淡的语法糖. 他必须仍然遵守no effects的约定.Ulf Wigert提到他对Paramazied Modules的扩充

    -module(pmod, ).

    -export().

    sum() -> X + Y.

    set_Y(NewY) -> {?MODULE, X,NewY}.

    Eshell V5.5.4.3 (abort with ^G) 1> 1> c(pmod). {ok,pmod} 2> M = pmod:new(3,4). {pmod,3,4} 3> M:sum(). 7 4> M1 = M:set_Y(5). {pmod,3,5} 5> M1:sum(). 8

    M1=M:set_Y(5) 这一点非常重要,如果允许M:set_Y(5),M:sum()输出8的话那么整个Erlang就分崩离析

评论功能维护中,当前仅展示历史评论。