D
Dik T. Winter
>
> I had that problem licked a decade ago, when I developed an assertion
> system against multiple side effects in a macro expansion, for the
> paranoid. This is a paper tiger.
I think not, but see below.
>
> This proliferation is not an adequate replacement for proper
> polymorphism, like what we have in the operators.
Yes, you get that if you can not define your own polymorphism. C does not
allow it and so we can not get it properly.
> Though it exorcises the demon of multiple evaluation, it has pitfalls
> of its own. If you change the types of some arguments, you have to edit
> all of the calls. If you don't edit some of them, they will compile
> anyway, and so you may be calling imax on a pair of doubles.
RIght. The same holds if you change the type of an argument of any procedure.
It may compile with or without warning and can give wrong results or not.
> This is like going back to the B language, which had separate operators
> for floating math.
Ah, back to operators, but in C they are polymorphic by definition. In other
languages also functions can be polymorphic.
>
> A macro that evaluates twice, but exhibits type polymoprhism, is a
> better alternative than a proliferation of different max functions,
> in spite of the multiple evaluation risk of the macro version.
You can state that as an absolute, but it makes no sense as such. What
about the 'abs' function (which is *not* a macro and does not exhibit
type polymorphism)?
> Let's see, remember one simple thing---don't place side effects in the
> argument---versus remember which member of proliferation of functions to
> use when, and keep revising the choice when the surrounding code
> changes. Hmm.
When you change types (especially arithmetic types) you have *always* to
consider such things. Consider:
int a = 1;
int b = 3;
float c = 3;
int d;
d = (a / c) * b;
Do you not expect to change the code when the type of c is changed from
float to int because c can have integral values only?
Also consider:
void dmaxv(double *a, double *b, double *res, int n) {
while(n-- > 0) *res++ = dmax(*a++, *b++);
}
well known idiom in some circles (so much so that it was a single instruction
on the CDC Cyber 205). You would prefer it as:
void dmaxv(double *a, double *b, double *res, int n) {
while(n-- > 0) {
*res = MAX(*a, *b);
res++; a++; b++;
}
?
(And, yes, the field I come from is numerical mathematics where polymorphism
is much less a requirement than single evalutation.)