Erlang中的参数化模块有哪些替代方案? [英] What alternatives are there to parameterised modules in Erlang?

查看:102
本文介绍了Erlang中的参数化模块有哪些替代方案?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以看到为什么使用参数化模块,因为它们允许我们执行以下操作:

I can see why parameterised modules are used so much, as they allow us to do things like:

X = y:new("param").

X:action1().
X.get_property():

:感觉非常OO。然而,这只是Erlang的一个实验性功能,我听说可能会被删除,所以我需要找到一个替代方案。

: which feels very OO. However, this is only an experimental feature in Erlang and I hear it may be removed, so I need to find an alternative.

推荐答案

参数化模块只不过是一个功能的第一个参数的捷径。看这两个例子:

Parametrised modules are nothing but a shortcut on the first argument of a function. See these two examples:

-module(y, [Name,Age]).
-compile(export_all).

action1() -> io:format("Hello, ~p!~n",[Name]).

get_age() -> Age.

运行它:

1> c(y).
{ok,y}
2> X = y:new("Fred",22).
{y,"Fred",22}
3> X:action1().
Hello, "Fred"!
ok
4> X:get_age().
22

现在没有参数化的东西:

Now without parametrized stuff:

-module(y).
-compile(export_all).

action1([Name,_]) -> io:format("Hello, ~p!~n",[Name]).

get_age([_,Age]) -> Age.

运行它:

1> c(y).
{ok,y}
2> X = ["Fred",22].
["Fred",22]
3> y:action1(X).
Hello, "Fred"!
ok
4> y:get_age(X).
22

参数化模块最大的优势是您转移携带的负担状态从变量到模块名称。对于不习惯Erlang方式的人来说,这看起来要简单得多,但与传统风格的代码相冲突。

The biggest 'advantage' of parametrized modules is that you shift the burden of carrying state from a variable to a module name. This is in appearance much simpler to someone not used to 'the Erlang way', but it clashes with conventional styles of code.

这不仅仅是一个实验性的问题,不。你抛出参考透明度,不可变变量的语义变得有点奇怪。一个很好的例子是想象你将以下函数添加到参数化模块中:

It's not just a question of being experimental or not. You throw out referential transparency and the semantics to immutable variables become a bit weird. A good example of this is to imagine you add the following functions to a parametrized module:

ret_fn() -> fun(Age) -> Age + 5 end.

编译模块时,会收到警告 ./ y.erl: 8:警告:变量'年龄'阴影在'乐趣'。这是警告您,您使用匿名函数的head子句中的预定义变量的名称。然而,快速查看 ret_fn / 0 函数绝对不会显示该变量来自哪里。

When compiling the module, you get the warning ./y.erl:8: Warning: variable 'Age' shadowed in 'fun'. This is warning you that you are using the name of a predefined variable within the head clause of an anonymous function. However, a quick look at the ret_fn/0 function shows absolutely NO sign of where that variable is from.

现在想象一下,您可以使用变量 Name 作任何其他用途;你会得到一个运行时错误告诉你 **错误:没有匹配的右手边的值< ...>

Now imagine that you use the variable Name for any other purpose; you will get a run time error telling you ** error: no match of right hand side value < ... >.

我所做的一切是,参数化的模块减少了你需要付出的代价的逻辑简单。不只是你,而是任何其他Erlang程序员都可以使用你的代码。

The point I'm making is that parametrized modules reduce the amount of typing you need to do at the expense of logical simplicity. Not just for you, but for any other Erlang programmer to work on your code.

除此之外,透析器,TypEr,tidiers等工具不能保证支持这些习语。这些工具也很有用!不要解雇他们。 (编辑:更新版本的Erlang(R13B04 +)现在保证支持)

On top of that, tools like dialyzer, TypEr, tidiers and whatnot have no guarantee to support these idioms. These tools are pretty useful, too! Don't dismiss them. ( newer versions of Erlang (R13B04+) now guarantee that support)

参数化模块的最佳替代方法是避免它们,并使用mochiweb之外的其他任何Erlang程序员正在使用。

The best alternative to parametrized modules is to avoid them and use what every other Erlang programmer outside of mochiweb is using.

这篇关于Erlang中的参数化模块有哪些替代方案?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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