[erlang-questions] noob namespace question

Thomas Allen thomas@REDACTED
Wed Dec 12 19:38:56 CET 2012


On Wed, Dec 12, 2012 at 01:30:03PM -0500, Thomas Allen wrote:
> This is not valid, and you will get a "bad function" error if you try to
> pass your function this way. You must create a "fun" like so:

Note that there are ways to make atoms as functions usable. You will
have to export functions you are calling this way, even if they are
internal to your module:

  -module(foo).
  
  %% API.
  -export([main/0]).
  
  %% Private.
  -export([a/0, b/0]).
  
  %% API.
  
  main() ->
      test1(a) + test2(b).
  
  %% Private.
  
  test1(Fn) -> ?MODULE:Fn().
  
  test2(Fn) -> apply(?MODULE, Fn, []).
  
  a() -> 1.
  
  b() -> 2.

You will see that:

  1> foo:main().
  3

These are two ways to call a function with nothing but an atom.

Thomas Allen



More information about the erlang-questions mailing list