gen_server:handle_call & -ifdef

Raimo Niskanen <>
Fri Nov 3 10:26:18 CET 2000


Hi Gerry,

there is some documentation in "Erlang Extensions since 4.4", link from
http://erlang.org/doc/r7b/doc/index.html. This, unfortunately,  says
nothing about the behagiour You encountered.

There is also the Erlang specification, that slowly is getting out of
date but still is the best there is. It is found at
http://erlang.org/download/erl_spec47.ps.gz, gzipped Postscript, sorry.
There the behaviour is stated, perhaps not too clearly, in sections 7.1
and 7.4.

In short, the preprocessor reads a sequence of TokenSequences. A
TokenSequence is terminated by a full stop ("."), so an "-ifdef()."
directive is a TokenSequence, and an "-endif." too, and what comes
before, in between and after must also be terminated with a full stop.

Too bad.

One standard trick is to conditionally define a debug printout macro
like this:

	-ifdef(erlang_debug).
		-define(dbg_out(Term), io:format("~p~n", [Term])).
	-else.
		-define(dbg_out(Term), ok).
	-endif.

, but that won't help You, but a similar trick might:

	-ifdef(erlang_debug).
		-define(handle_call_aaa, 
			handle_call(aaa, From, State) ->
				Reply = aaa,
				{reply, Reply, State};).
	-else.
		-define(handle_call_aaa, ).
	-endif.
	.
	.
	?handle_call_aaa
	handle_call(Request, From, State) ->
		Reply = ok,
		{reply, Reply, State}.

, or this more generic macro hackery:

	-ifdef(erlang_debug).
		-define(dbg(X), X).
	-else.
		-define(dbg(X), ).
	-endif.
	.
	.
	?dbg(handle_call(aaa, From, State) ->
		begin
			Reply = aaa,
			{reply, Reply, State}
		end;)
	handle_call(Request, From, State) ->
		Reply = ok,
		{reply, Reply, State}.

this is on thin ice, however. E.g You need the begin .. end construct to
keep together the argument to the dbg macro, and You cannot have more
that one guard since then You need commas, which split the argument to
the dbg macro into two. That can be solved with a dbg macro with more
arguments, for the guards, and now it gets really ugly.

Good luck, anyway.

/ Raimo Niskanen, Erlang/OTP



Gerald Biederbeck wrote:
> 
> Hi,
> 
> I wrote a gen_server and tried to compile it:
> the source code is something like this:
> 
> >       -ifdef(erlang_debug).
> >
> >               handle_call(aaa, From, State) ->
> >                       Reply = aaa,
> >               {reply, Reply, State};
> >
> >       -endif.
> >
> >       handle_call(Request, From, State) ->
> >               Reply = ok,
> >       {reply, Reply, State}.
> 
> The error I got was:
> 
> >       ./ifdef.erl:91: unterminated '-ifdef'
> 
> I assume that it must have something to do
> with "not ending with a dot" before '-endif.',
> because the following works:
> 
> >       -ifdef(erlang_debug).
> >
> >               handle_call(aaa, From, State) ->
> >                       Reply = aaa,
> >               {reply, Reply, State};
> >
> >               handle_call(Request, From, State) ->
> >                       Reply = ok,
> >               {reply, Reply, State}.
> >
> >       -else.
> >
> >               handle_call(Request, From, State) ->
> >                       Reply = ok,
> >               {reply, Reply, State}.
> >
> >       -endif.
> 
> Is there any documentation for this '-ifdef().'???
> Is the second way as I am supposed to do it?
> 
> Any help and comments are welcome!
> 
> Regards
> /Gerry



More information about the erlang-questions mailing list