On Sun, 8 Jun 2003, Frank Heckenbach wrote:
Mirsad Todorovac wrote:
On Sat, 7 Jun 2003, Russell Whitaker wrote:
On Sat, 7 Jun 2003, Mirsad Todorovac wrote:
I apologize for replying to my own email, but is there a way to do this with GPC preprocessor, or is it on list of planned features?
{$if Low (Char) < 0} {$error "this won't work: negative Char used as index} {$endif}
Perhaps I'm wrong, but I thought by definition a Char cannot be negative.
Of course, it cannot. It's not even a number. So, if anything `Ord (Low (Char)) < 0' or `Low (Char) < Chr (0)' would work. But (a) it's always False, and (b) ...
OK; I'm still fresh from C. Excuse my ignorance ...
OK, then the second one:
{$if BitSizeOf (LongestInt) > High (TString)} {$error "this won't work: on this platform LongestInt it too huuge ..."} {$endif}
... (apart from the fact that this check is really quite silly, as we've discussed privately) ...
Particular example, sure. In general, it would be nice to check for "impossible" prerequisites. This just reminded me of an Arriane counter that "possibly couldn't go over 32767" (or something) ;-) ...
Generally, and theoretically, when I'm writting part of unit/library, I can't know in advance what it should be running on. Pehaps there are machines with BitSizeOf (LongestInt) > High (TString) = 2048 already ...
Obviously, even now when I'm looking better, preprocessor may not know about SizeOf(), BitSizeOf(), High() and Low() of a user-defined type :-/ ?!?
... it does not know anything about Pascal. It's a *pre*processor!
Of course.
You can do something like compile-time assertions by something like:
const AssertTWindowXYSize = 1 / Ord (CONDITION);
This will yield a division by zero and fail at compile time if the condition is false.
Clever.
I've wondered if we should build in such a feature with some syntax, but I'm not sure if the syntax above or a new syntactic extension would be better ...
The first thought is a macro
{$define CompileTimeAssert(x) (1 / Ord (x))}
but it could be used only as a function assigned to a dummy constant.
Mirsad