[erlang-questions] implementing annotation in erlang

Fred Hebert (MononcQc) mononcqc@REDACTED
Thu Sep 3 01:39:38 CEST 2009


This solution might not be the nicest and will be slower than most,
but it should require less maintenance and code to use:

We'll use a custom module attribute to get the functions we want to
map. Let's create and compile the following module to test our stuff:


-module(chk).
-export([test/0,fake/1]).

-command_map([test/0]).

fake(N) -> N+1.
test() -> test.


We compile it and can fetch its module attributes with:

10> proplists:get_value(command_map, chk:module_info(attributes)).
[{test,0}]


Maybe that can help you at least getting the names of the functions
where you need them.

If you'd like a central way to find all the functions inside
command_map module attributes, you might want to try the following:


-module(mappings).
-export([find/0]).

-define(ATTR, command_map).

find() ->
[ {Mod, get_mapped(Mod)} || {Mod,_Path} <- code:all_loaded(), is_mapped(Mod)].

is_mapped(Mod) ->
is_list(proplists:get_value(?ATTR, Mod:module_info(attributes))).

get_mapped(Mod) ->
proplists:get_value(?ATTR, Mod:module_info(attributes)).



Which will scan through all loaded modules (yeah, they need to be
active already...) and fetch the mapped functions. It'll return the
whole list under the form [{Mod1, [{Fun1,Arity1},{Fun2,Arity2},
...,{FunN,ArityN}]}, {ModN, ...}], Which should make it easier to call
stuff. The scheme is easy enough to modify, process further to attach
to a keyword and whatnot.

I've attached both files just in case. Hope this helps.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: chk.erl
Type: application/octet-stream
Size: 103 bytes
Desc: not available
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20090902/2ce00c5e/attachment.obj>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: mappings.erl
Type: application/octet-stream
Size: 340 bytes
Desc: not available
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20090902/2ce00c5e/attachment-0001.obj>


More information about the erlang-questions mailing list