James Kuyper said:
On 11/18/2012 08:36 PM, BartC wrote:
The header files are compiled separately as part of each translation
unit. It's even possible, and in some contexts useful, to have them
compile differently in different translation units. They're basically
just a technique for getting identical text into different translation
units.
I do the same; that 'import module' stuff just boils down to including a
file in the same way that C does it.
The difference is that I allow that file to be created automatically as part
of how the language works. If that feature wasn't available, I could write
that file by hand, or code it directly, just like you have to do in C.
Conversely, you could bolt-on some utility to C to construct certain headers
automatically, which I'm sure is already done (and as I've done with
function prototypes).
So, if I knew there was an array X in module B of length 10, I could write
this in module A:
#define XLEN 10
but I'd need to keep it up-to-date every time B changed. (Of course if the
sources to B were hidden, then I wouldn't know this unless the authors
explicitly made this known, via docs or via an include file (in which case I
might not need my #define).)
Effectively, we've managed to export a local array size, a compile-time
constant, from a module. We're arguing about the mechanisms for doing so
automatically.
The implications are quite different when the things that can be known
at compile time are different. A #defined constant in a shared header
file can be known at compile time in C (and could be different in
different translation units for the same header file, if set up
properly).
Yes, that's a feature of C that makes it so easy to compile! (When a #define
expression includes __LINE__ for example.)
The value of an object with external linkage that is defined
in some other translation unit cannot be known at compile time in C, and
cannot (with defined behavior at least) be different in different
translation units, no matter what fancy tricks you use. The closest
equivalents in your language works quite differently, and that changes
the implications of "compile time".
Well actually I'm not so interested in the values of objects that use
storage, not at compile time anyway. That is an issue for C because it's
what C uses for 'const ints', part of the OP's problem.
(If 'proper' named constants were used (even enums), then the problem
wouldn't arise, and C doesn't allow enums to be external.)
However, if the value of such an object, in the module where it's defined,
is determinable by the compiler to be constant, then there's no reason why
that information can't be 'exported' in the same way that a plain number
could be.
Although I agree a lot of the existing features of C can make that much more
difficult than it need be. In that case the compiler would need to be able
to determine whether that value was also invariant (be the same on repeated
compilations), not defined recursively (perhaps defined in terms of itself
via a chain of extern statements!) and so on. So perhaps in this case it's
more trouble than it's worth.