如何在Erlang中动态调用模块?

如何在Erlang中动态调用模块?,erlang,Erlang,假设我有两个模块a.erl和b.erl。两个模块都包含相同的函数(在Java中,我会说“两个类实现相同的接口”)。 在模块“c.erl”中,我希望有一个返回模块“a”或“b”的函数(取决于参数) 这是我想在模块c.erl -module(c) get_handler(Id) -> % if Id == "a" return a % if Id == "b" return b test() -> get_handler("a"):some_function1("here

假设我有两个模块a.erlb.erl。两个模块都包含相同的函数(在Java中,我会说“两个类实现相同的接口”)。 在模块“c.erl”中,我希望有一个返回模块“a”或“b”的函数(取决于参数)

这是我想在模块c.erl

-module(c)

get_handler(Id) ->

 % if Id == "a" return a

 % if Id == "b" return b

test() ->

 get_handler("a"):some_function1("here were go for a"),

 get_handler("a"):some_function2("aaaa"),

 get_handler("b"):some_function1("here we go for b")

我怎样才能做到这一点?我对Erlang比较陌生,不知道怎么做。在Java中,这一点非常明显,因为您只需返回类的新实例。

只需使用
get\u handler/1
将模块名作为原子返回,然后使用它调用所需的函数:

(get_handler("a")):some_function2("aaaa"),
(get_handler("b")):some_function1("here we go for b").
请注意,在本例中,对
get\u handler/1
的调用需要括号

模块
A
b
get\u handler/1
的简单版本可以是:

get_handler("a") -> a;
get_handler("b") -> b.

如果变量中有一个原子,则可以将其用作模块名

因此,您可以这样定义
c:get\u handler/1

get_handler("a") -> a;
get_handler("b") -> b.
test() ->
    (get_handler("a")):some_function1("here were go for a"),
    (get_handler("a")):some_function2("aaaa"),
    (get_handler("b")):some_function1("here we go for b").
-behaviour(some_behaviour).
-module(some_behaviour).
-callback some_function1 (String :: string()) -> ok .
-callback some_function2 (String :: string()) -> ok .
您的
c:test/0
看起来不错,只是需要额外的括号,如下所示:

get_handler("a") -> a;
get_handler("b") -> b.
test() ->
    (get_handler("a")):some_function1("here were go for a"),
    (get_handler("a")):some_function2("aaaa"),
    (get_handler("b")):some_function1("here we go for b").
-behaviour(some_behaviour).
-module(some_behaviour).
-callback some_function1 (String :: string()) -> ok .
-callback some_function2 (String :: string()) -> ok .
然后在模块
a
b
中,只需定义a
一些功能1/1
一些功能/2
,例如:

some_function1(Str) ->
    io:format("module ~s function some_function1 string ~s~n", [?MODULE, Str]).

some_function2(Str) ->
    io:format("module ~s function some_function2 string ~s~n", [?MODULE, Str]).
编辑:如果你要做这类事情,你可能还应该定义一个行为,顺便说一句,这意味着你将在模块
a
b
中声明如下内容:

get_handler("a") -> a;
get_handler("b") -> b.
test() ->
    (get_handler("a")):some_function1("here were go for a"),
    (get_handler("a")):some_function2("aaaa"),
    (get_handler("b")):some_function1("here we go for b").
-behaviour(some_behaviour).
-module(some_behaviour).
-callback some_function1 (String :: string()) -> ok .
-callback some_function2 (String :: string()) -> ok .
然后创建模块
一些行为
如下:

get_handler("a") -> a;
get_handler("b") -> b.
test() ->
    (get_handler("a")):some_function1("here were go for a"),
    (get_handler("a")):some_function2("aaaa"),
    (get_handler("b")):some_function1("here we go for b").
-behaviour(some_behaviour).
-module(some_behaviour).
-callback some_function1 (String :: string()) -> ok .
-callback some_function2 (String :: string()) -> ok .

这意味着任何像
a
b
这样声明它们支持行为
的模块都必须定义这些函数,如果它们不支持,编译器会大声说出来。这里还定义了参数类型和返回值,用于静态分析等。

我不确定我是否理解这个问题的写作方式。您是否可以这样导入:-
导入(模块,[Function1/Arity,…,FunctionN/Arity])。
然后调用
a:some\u function/Arity
b:some\u function/Arity
?我不希望在模块“c”或“if”语句中有不同的调用。我想让调用函数的代码与get\u处理程序返回的函数相同。在我的示例中,get_handler在“a”和“b”之间进行选择,但可能有更多的模块可供选择。非常感谢您的回答。这正是我需要的。我的问题解决了,非常感谢你的回答。这正是我需要的。我的问题解决了。