Re: Several questions on Objective C
Re: Several questions on Objective C
- Subject: Re: Several questions on Objective C
- From: Michael Gersten <email@hidden>
- Date: Sun, 07 Apr 2002 12:34:54 -0700
I have to respond to this one. Sorry.
>
> Yes, they will have to unlearn some principles, like templates (which are
>
> only a hack to make up for the lack of run time binding), but most OO
>
> principles in C++ are valid and do apply to Obj-C.
>
>
No, templates are useful also to limit code duplication. (Think about
>
complex<float> vs. complex<double>.) Something you can do with macros, but
>
it is more complicated and dangerous.
Templates don't limit _executable_ code duplication; they _cause_ it.
They limit _source_ code duplication only.
So what is the big deal of complex<float> and complex<double>?
How would I implement complex in ObjC to allow for that?
NeXT used the NSNumber cluster to hide umpteen different subclasses.
Using a class cluster to hide Complex_xxx under a single Complex isn't the goal.
One way is to make a NSComplex class that takes real and imaginary as NSNumbers.
If I wanted to use a template-like feature, in ObjC, well...
This goes back a number of years. It was last tested in pre-ansi C. It worked wonders then, but was too much like the obfuscated C contest winners, so I stopped doing it.
(This is typed into the mail window, test before using)
(OK, as I type it in, it's a _LOT_ cleaner than it was in plain C. There's only one or two "token hacks", thanks to overloaded message names instead of functions and structures.)
code.m:
#define COMPLEX_CLASS double
#include "complex_interface.h"
#include "complex_implementation.h"
#define COMPLEX_CLASS float
#include "complex_interface.h"
#include "complex_implementation.h"
main()
{
Complex_float *f1, *f2, *f3;
Complex_double *d1, *d2, *d3;
f2 = ...
f3 = ...
f1 = [f2 times: f3];
...
}
complex_interface.h:
#ifndef COMPLEX_CLASS
#error Must define a class type before including me
#endif
@interface Complex_ ## COMPLEX_CLASS // attempting to re-write for ansi-C, test this!
{
COMPLEX_CLASS real, imag;
}
- add: other;
- times: other;
...
@end
complex_implementation.h:
@implementation Complex_ ## COMPLEX_CLASS
...
@end
An excersize to the reader: Put in a protocol for Complex_API. Define the arguments and returns to take that protocol. Add 'real' and 'imaginary' methods to the protocol (so that you can mix Complex_float and Complex_double) items. Hmm... give those methods type 'double', so you can mix them.
You get zero source code duplication, just like C++.
You can write the code as real routines, not inlined routines.
You generate the executable code _ONCE_ -- C++'s 'everything is inlined' means that the executable code repeats many, many times.
You do have to take care to only generate the templates from a single source file -- otherwise, you will get the code defined multiple times. (only include the _implementation.h file ONCE per project.)
For this particular example, I'd go for a complex that takes NSNumbers. Zero duplication that way.
Michael
p.s. Operator overloading is the one saving grace of C++. Think quadratic roots. Think matrix/vector math.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.