Erlang Parameterized Modules

Parameterized Modules also called Abstract Modules or Tuple Modules.

From Stack Overflow

1
2
3
4
5
6
7
-module(foo).
-compile(export_all).
new(Bar) -> {foo, Bar}.
get({foo, Bar}) -> Bar.
1
2
3
4
5
6
1> c(foo).
{ok,foo}
2> Foo = foo:new(bar).
{foo,bar}
3> Foo:get().
bar

From Elang Central

1
2
3
4
5
6
7
-module(test, [A, B]).
-export([get_a/0, get_b/0]).
get_a() -> A.
get_b() -> B.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
1> c(test).
{ok,test}
2> test:module_info().
[{exports,[{get_a,1},
{get_b,1},
{new,2},
{instance,2},
{module_info,0},
{module_info,1}]},
{imports,[]},
{attributes,[{vsn,[44232957561630721650243949532469790901]},
{abstract,[true]}]},
{compile,[{options,[]},
{version,"4.6.4"},
{time,{2010,2,21,20,52,18}},
{source,"/home/bile/code/erlang/test/test.erl"}]}]
3> T = test:new("This is A", "This is B").
{test,"This is A","This is B"}
4> T:get_a().
"This is A"
5> T:get_b().
"This is B"
6> T:module_info().
** exception error: bad argument
in function erlang:get_module_info/2
called as erlang:get_module_info(test,{test,"This is A","This is B"})
in call from test:module_info/1
7> test:instance("A","B").
{test,"A","B"}
8>

Ending

栗子分别来自于
http://stackoverflow.com/questions/40539120/are-tuple-modules-an-officially-documented-feature-of-the-language

http://www2.erlangcentral.org/wiki/?title=Parameterized_Modules

官方并不建议使用这种方式。