抑制 Erlang“未使用的函数”警告
我编写了一个 Erlang 模块,其中并非所有内部函数都被直接调用。相反,有几个函数看起来像这样:
weird_func(Cmd, Args) ->
?MODULE:Cmd(Args).
这是一个简化的示例,但您明白了。 Erlang 编译器会发出有关未使用函数的警告,而实际上它们确实被使用了,只是没有直接使用。有什么办法可以抑制这些警告吗?理想情况下,我不想抑制所有此类警告,而是想告诉 Erlang 编译器将一些特定函数视为特殊情况。
I wrote an Erlang module where not all the internal functions are directly called. Instead, there's a couple functions that look something like this:
weird_func(Cmd, Args) ->
?MODULE:Cmd(Args).
It's a simplified example, but you get the idea. The Erlang compiler spits out warnings about unused functions, when in fact they are actually used, just not directly. Is there some way to suppress these warnings? Ideally I don't want to suppress all such warnings, but rather I'd like to tell the Erlang compiler to consider a few specific functions as special cases.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
有一个专门用于此的编译选项:
http://www.erlang.org/doc/man/compile.html
因此,对于您的示例,插入这样的指令:
但是上面关于编译器默默丢弃函数的警告仍然存在
据我所知。
there is a compile option specifically for this :
http://www.erlang.org/doc/man/compile.html
so, for your example insert a directive like this :
but the above warning about the compiler silently discarding the functions still stands
as far as i know.
这不仅仅是压制警告的问题。无法按照您想要的方式调用未导出的函数。
编译器可以完全删除未使用的、未导出的函数。只需导出函数即可使用 ?MODULE:F 语法进行调用。
It's not just a matter of suppressing the warning. An unexported function can't be called in the way you intend.
The compiler is free to drop the unused, unexported function completely. Simply export the function making it available call using the ?MODULE:F syntax.
如果函数既未导出,也未显式调用,则将其报告为未使用。因此,您有两种方法可以选择:
1)导出间接使用的函数。如果您不希望从模块外部调用这些函数,您可以在文档(和注释中)突出显示这一点。
2)在 odd_func 中显式调用每个函数:
后者有点冗长,但它将提供如果用户调用不存在的函数,则会出现更好的错误
If a function is neither exported, nor explicitly called, it is reported as unused. So you have two ways to go:
1) Export the functions that are used indirectly. If you don't want those functions to be called from outside the module, you can highlight this in the documentation (and in comments.)
2) Call each function explicitly in weird_func:
This latter is a bit more verbose, but it will provide better error for the users, if they call a non-existing function
你基本上有两个选择。
第一个是导出函数,从而“使用”它们。这对于您的原始示例的工作也是必要的。
第二是使用类似以下内容在本地调用函数:(
请注意,我的示例无法编译。您必须根据您调用的函数将 Args 参数分成几部分。)
You basically have two options.
1st is to export the functions, thus "using" them. This is also necessary for your original example to work.
2nd is to call the functions locally using something like:
(Note that my example doesn't compile. You have to split the Args argument into pieces depending on which func you are calling.)
您还可以以
fun func1/3
格式传递Cmd
,例如:weird_func(fun func1/3, [1, 2, 3])
这样函数就不会被删除。
编辑:刚刚添加了我的评论,以便对未来的读者有用
You can also pass
Cmd
in thefun func1/3
format, for example:weird_func(fun func1/3, [1, 2, 3])
This way the function will not be dropped.
Edit: Just added my comment so it can be useful for future readers