`
javatoyou
  • 浏览: 1018900 次
  • 性别: Icon_minigender_2
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

Erlang平面名字空间的缺陷及解决方案

阅读更多
问题缘起:参阅 Erlang Question 邮件列表上的讨论(http://groups.google.com/group/erlang-questions/browse_thread/thread/fed4e293a015ce8)。以下回复于该邮件列表。由于用的英文还是比较粗浅,我就不费神去翻译了。

I think flat module namespaces is a defect of erlang design.

For example, I write a foo.erl, And it works well. But maybe in a late erlang version (eg. R13B) also write such module named foo.erl. Then, you can see my application goes wrong.

How to avoid things like this? Let's see the following ways:

1. Adjust module searching paths, and let user path (which contains my foo.erl) take precedence over erlang stdlib/otp path. But, this way can't always work well. If some other stdlib/otp modules use system foo.erl (not my foo.erl), Things goes wrong.

2. Write erlang modules always named with a prefix (a fake namespace. For example, projectname_foo.erl or organization_projectname_foo.erl). This way really can solve the problem. But, It seems ugly.

Is there a way let's user still can call foo:func (not call foo.erl provied by stdlib/otp, but my projectname_foo.erl)? I have a suggestion:

Can erlang provide a 'module name alias'? That is, I can rename a module's name temporarily in a module? For example:

-module(a_module_that_call_my<wbr></wbr>_foo).
-alias(foo, organization_projectname_foo). %% alias

some_func_call_foo() ->
foo:func(). %% same as: organization_projectname_foo<wbr></wbr>:func()

Currently I can do this by using the 'define' keyword. For example:

-module(a_module_that_call_my<wbr></wbr>_foo).
-define(Foo, organization_projectname_foo). %% alias

some_func_call_foo() ->
?Foo:func().

It works well, but a bit ugly.
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics