我有一个带有函数的模块,这些函数返回不同的proplist。模块函数如下所示:
get_schema(auth) ->
[
{<<"login">>, [{string, required}]},
{<<"password">>, [{string, required}]},
{<<"id">>, [{string, required}]}
];
get_schema(update_password) ->
[
{<<"oldpassword">>, [{string, required}]},
{<<"newpassword">>, [{string, required}]},
{<<"id">>, [{string, required}]}
].然后,我从另一个模块导入此模块,并使用相应的函数获取proplist。
是否可以将这些proplist保存为变量,而不是函数,以便从导入此函数的其他模块访问?
发布于 2016-08-10 23:03:34
我建议让它保持现在的样子。
如果你真的想使用变量,那么只需将返回值赋给一个变量,并这样使用它:
AuthSchema = my_module:get_schema(auth),这个函数在引用上是透明的,这意味着调用它和使用它的返回值会产生相同的结果。在Erlang中,对常量使用单独的模块和不带参数的函数(或使用模式匹配,如您的示例)是相当常见的做法。例如,看看translit函数here。
https://stackoverflow.com/questions/38876615
复制相似问题