【问题标题】:Suppressing Erlang “unused function” warnings in header files抑制头文件中的 Erlang “未使用函数”警告
【发布时间】:2015-10-16 18:50:57
【问题描述】:

我有一个包含一些功能的头文件。 它的目的是为了可扩展性;)

在这个头文件中,我使用-spec 作为我的函数。 因为我的头文件包含在多个 erl 文件中,并且具有一些并非所有 erl 文件都使用的功能,所以我收到未使用函数的警告(正在使用但在另一个 erl 文件中)。

例如:

对于这个示例,我收到一个警告,模块 a 未使用函数 headerExecute,模块 c 未使用函数 headerCount。

%% Shell %%
BPid = b:create(),
APid = a:create(BPid),
CPid = c:create(BPid).

-module(a).
-export([create/1, count/2, stop/1]).
-export([loop/1]).
-include("header.hrl").

create(Id) ->
  spawn(?MODULE, loop, [Id]).

count(Pid, Counter) ->
  Pid!{count, Ref = make_ref(), self(), Counter},
  receive
    {Ref, Result} -> Result
  end.

stop(Pid) ->
  Pid!{stop}.

loop(Id) ->
  receive
    {count, Ref, From, Counter}
      Result = headerCount(Id, Counter),
      From!{Ref, Result},
      ?MODULE:loop(Id);
    {stop} ->
      ok;
    _ ->
      ?MODULE:loop(Id)
  end.

-define(header, header).
-spec headerCount(pid(), integer()) -> integer().
-spec headerExecute(pid(), atom()) -> no_return().

headerCount(Pid, Counter) ->
  Pid!{count, Ref = make_ref(), self(), Counter},
  receive
    {Ref, Result} -> Result
  end.

headerExecute(Pid, Cmd) ->
  Pid!{execute, Cmd}.

-module(b).
-export([create/0, stop/1]).
-export([loop/0]).

create() ->
  spawn(?MODULE, loop, []).

stop(Pid) ->
  Pid!{stop}.

loop() ->
  receive
    {count, Ref, From, Counter} ->
      From!{Ref, Counter + 1},
      ?MODULE:loop();
    {execute, Cmd} ->
      %% Execute something with the command Cmd %%
      ?MODULE:loop();
    {stop} ->
      ok;
    _ ->
      ?MODULE:loop()
  end.

-module(c).
-export([create/1, execution/2, stop/1]).
-export([loop/1]).
-include(header).

create(Id) ->
  spawn(?MODULE, loop, [Id]).

execution(Pid, Cmd) ->
  Pid!{execution, Cmd}.

stop(Pid) ->
  Pid!{stop}.

loop(Id) ->
  receive
    {execution, Cmd} ->
      headerExecute(Id, Cmd),
      ?MODULE:loop(Id);
    {stop} ->
      ok;
    _ ->
      ?MODULE:loop(Id)
  end.

为了可扩展性,不能在 erl 文件中导出这些函数,而在头文件中导出这些函数会导致错误。

如何丢弃或消除这些警告?

编辑:我将此功能放在标题中,以便可以轻松地将新的 erl 模块添加到代码中并调整现有的 erl 模块,而无需触及其他 erl 模块。

提前致谢。

【问题讨论】:

  • 您是否通过分析代码证明,在头文件中包含这些函数对应用程序的性能产生积极影响?如果没有,我建议将它们像普通函数一样放入普通模块中,这样可以消除警告。

标签: erlang


【解决方案1】:

在头文件中使用nowarn_unused_function 编译器参数。

-compile({nowarn_unused_function, [headerCount/2, headerExecute/2]})

但它没有任何可扩展性。你这个词用错了。

【讨论】:

  • 我将可扩展性更改为可扩展性。对不起,错过了解释。
  • 你的意思是效率,不是吗?因为您可以将headerCount/2headerExecute/2 放入模块并在外部调用它。
猜你喜欢
  • 2011-03-19
  • 2014-04-24
  • 2010-12-24
  • 2016-11-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-23
相关资源
最近更新 更多