[erlang-questions] Compilation question...

Per Gustafsson per.gustafsson@REDACTED
Tue Jan 30 16:00:05 CET 2007


Emil Hellman wrote:
> Hello!
> 
> I have a small feature I want to several modules. My thought is to add
> this feature if I encounter a '-myfeature(Args)'  in the module.
> 
> Example:
> 
> -module(foo).
> -export([bar/0]).
> -myfeature(Args).
> 
> Where args could be several different things.
> 
> So essentially if I find this "tag" in the module I would like to
> append some code to the file (dependant on the args). And then compile
> the modified module. However, I don't want the generated code to be
> appended to the actual module, simply added temporarily during the
> compilation.
> 
> To do this I need some guidance. What module(s) contain the code that
> is used by the compiler? I checked out 'compile.erl', but I think I
> want to do something to the modules before this code is called (though
> I might be wrong here).

If you do not want to use a parse transform you can try to use an 
include directive and the preprocessor to solve your problem. It would 
look something like this:

-module(foo).
-export([bar/0]).

-define(FEATURE_TYPE1,true).
-define(ARGS,Args).
-include("myfeature.hrl").
...

and in myfeature.hrl:

-ifdef(FEATURE_TYPE1).
Code to append using ?ARGS
-endif.

This of course less versatile than a parse transform, but it is easier 
to implement.

Per



More information about the erlang-questions mailing list