Dialects, C++, Overloading

Rob Carriere rob at raksha.eng.ohio-state.edu
Tue Feb 20 04:34:11 AEST 1990


In article <5137:06:46:38 at stealth.acf.nyu.edu> brnstnd at stealth.acf.nyu.edu
(Dan Bernstein) writes: 
>Rob, the issue is not user-defined operator syntax. The issue is overloading.

Yes.  Sorry I didn't make myself more clear.  I was trying to point out that
you can get just as confused without overloading.  I point that I _did_ miss
was that you are also talking about function overloading, not just operator
overloading.  That makes part of my last post besides the point.  Thanks for
clarifying. 

>Say a language provides a vector library with a vector addition routine.
>You might write ``a = b + c'' in full as ``a = b vec.+ c''. Overloading
>means abbreviating vec.+ and string.+ and matrix.+ and complex.+ as a
>single operator. We're not asking you to write a = vec.plus(b,c); we're
>pointing out the problems of ambiguous abbreviation.

1] I don't have a problem with seeing a = b + c; here.  I know what vectors,
   matrices and complex numbers do when you add them.

2] This starts out nice, but the example I gave in my post is going to look
   like: 

x vec.= (A mat_vec.* x) vec.+ (B mat_vec.* u);

   or perhaps even:

x complex_vec.=               (A complex_mat__complex_vec.* x) 
                complex_vec.+ (B complex_mat__complex_vec.* u);

   I claim that x = A*x + B*u; is drastically more readable.

>My main concern is maintainability. I hate debugging Ada---particularly
>someone else's Ada---because I have to learn an entirely new language
>just to understand each program. I have to force myself to look
>suspiciously at each operator and function call, asking ``what are the
>types?'' and ``is it overloaded?'' If I don't read Ada so painfully, I'm
>liable to overlook an overloaded operator, automatically assuming that
>(for example) a = b + c has no side effects or that bar(x) + foo(y) is
>the same construction that I just debugged when I saw it in the last
>routine.

But how is this different from assuming that func in 

   frubs = func( fobble );

has no side-effects?  Or that the same line in the next file will mean the
same thing?

>I often have a lot of trouble finding the definition of an overloaded
>function---my searching tools are of no help when the syntax sucks and
>the definition could be in any of ten libraries. But this is only a
>lesser evil compared to an overloaded operator insidiously taking over
>the innocent plus sign while the programmer looks away.

See above.  I don't understand why the evil is lesser.

>> [...] we have a second set of conventions to deal with [apart from those of
>> the programming language]: those of the subject at hand.
>
>This is the root of the problem: you have to learn a new language just
>to read each new program. 

I would hope not.  If you know linear algebra, the notation a + b for vectors
a and b will have meaning to you.  If you do not know linear algebra and you
still have to maintain the program, notation is the least of your problems.

>When that new language doesn't even have the
>same semantics for a = b, the reader is in big trouble. In contrast, if
>overloaded functions and operators had to have a special syntax, they'd
>be an easy-to-notice, natural first spot to look for bugs. The special
>syntax doesn't have to be anything more than, e.g., an initial period;
>but it has to be there. 

OK, but this much weaker than you what started out with.  The first example now
becomes:

  x .= A.*x .+ B.*u;

that's a lot closer to readability than all those decorations we had first.

>If the language provided a standard way to
>expand all overloaded operators into unambiguous versions, life would
>be wonderful.

That seems an implementation issue to me.  As long as the language is
statically typed, figuring out which operator goes where can be done
statically.  So you implementation could provide you with some kind of
hypertext-like help for this expansion.  In fact, it could do that without the
language needing those periods.  Then, if you have a confusing piece of code,
you could tell your implementation to show you a fully disambiguated version
of the code.

SR
"And v2.0 will show you a correct version of the code..."
---



More information about the Comp.lang.c mailing list