[erlang-questions] Erlang programming question: "record abc already defined"

Bengt Kleberg <>
Mon Jul 9 12:27:02 CEST 2007


On 2007-07-08 23:56, Anthony Kong wrote:
> Hi, all,
> 
> It is a erlang question form a firsttime beginner. So, bear with me if
> the source of problem is obvious.

no problem, everybody was a beginner at some time.


> I got a bunch of message like this when I run erlc against a number of
> erl files. Here are some examples:
> 
> ./reqmgt.hrl:14: record project already defined
> ./reqmgt.hrl:14: record project already defined
> ./reqmgt.hrl:14: record project already defined
> ./reqmgt.hrl:14: record project already defined
> ./reqmgt.hrl:14: record project already defined
> ./reqmgt.hrl:14: record project already defined

next time, could you give the whole command line?
and perhaps just one example if they all look the same?


> Let's say I have these files regmgt.erl, regmgt.hrl, project.erl,
> abc.erl, def.erl.
> 
> Except for the hrl files, they are all

have you other hrl files, too?
there is only one hrl file in you example, but you mention ''hrl files''.


> 1) based on the gen_server_template.mini from Joe's book
> 2) project, abc, def contains logic related to a particular record
> class. So, project.erl knows how to handle -record(proect, {...})
> 3) I am a java programmer, so the design of the project.erl, abc, def
> are  basically to handle CRUD.
> 4) reqmgt.erl is an implementation following a Facade pattern.
> 5) each of these erl files has a line "-include (reqmgt.hrl)"
> 
> in reqmgt.hrl I have define  "-record(project, {...})" and abc, def  etc.
> 
> (Yes, I am using hrl as if it is a C include file but not knowing how
> to do "#ifndef" :-) )

in erlang you can do:

-ifndef(MNESIA_TABLES_HRL).
-define(MNESIA_TABLES_HRL, 1).

%% lots of defines/records/etc

-endif. % NIBS_MNESIA_TABLES_HRL defined

> So, questions:
> 
> 1) Is it wrong to use "-include" this way? If so, how to fix it?

since you get a compile time error it sure is wrong :-)
seriously, lots (a large majority, at a guess) of very good erlang 
programmers like to have records defined in common hrl files and shared 
in several erl files.
i do not like the idea and try to avoid it if i can.

> 2) Does this 'design' sound OK in erlang?

one possibility is to hide your record in an erl file and have table 
create/access functions. that helps when loading new modules after 
upgrading records.


bengt
-- 
Those were the days...
    EPO guidelines 1978: "If the contribution to the known art resides
    solely in a computer program then the subject matter is not
    patentable in whatever manner it may be presented in the claims."



More information about the erlang-questions mailing list