views:

128

answers:

3

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.

+3  A: 

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:

weird_func(fun1, [A1,A2]) ->
    fun1(A1, A2);
weird_func(fun2, []) ->
    fun2();
weird_func(fun3, [A1,A2,A3]) ->
    fun3(A1,A2,A3).

This latter is a bit more verbose, but it will provide better error for the users, if they call a non-existing function

Zed
+3  A: 

It's not just a matter of suppressing the warning. An unexported function can't be called in the way you intend.

-module(foo).
-export([f/0]).
f() -> ?MODULE:g().
g() -> ok.

1> c(foo).
./foo.erl:4: Warning: function g/0 is unused
{ok,foo}
2> foo:f().
** exception error: undefined function foo:g/0

The compiler is free to drop the unused, unexported function completely. Simply export the function making it available call using the ?MODULE:F syntax.

cthulahoops
+2  A: 

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:

weird_func(Cmd, Args) ->
   case Cmd of
      func1 -> func1(Arg1, Arg2, Arg3);
      func2 -> func2(Arg1);
      ...

(Note that my example doesn't compile. You have to split the Args argument into pieces depending on which func you are calling.)

Daniel Luna

related questions