Objective C language question: Making init like a contrustor
Objective C language question: Making init like a contrustor
- Subject: Objective C language question: Making init like a contrustor
- From: Michael Gersten <email@hidden>
- Date: Sat, 25 May 2002 14:53:04 -0700
The basic question: Is it possible to design a runtime so that init's can act like constructors?
The details:
In C++, (I'm not sure about Java), when you are creating a new object, it first has an ISA of the root class, and the root class's constructor run; then the ISA is changed to the next class, and that class's constructor is run, etc.
In Objective C, the ISA is always the lowest class, even while the [super init]'s are running; this means that any message sent by those upper classes during init may wind up going down into a subclass that hasn't been initialized yet.
It isn't hard to imagine saying [self sequentialISA] at the start of an init when you want to have this sort of ISA behavior. Nor is it hard to imagine [self initFinished] just before the init returns, or [self initIsMessagable] when the object is initialized enough to receive messages intended for the current class -- IE, the init paradigm might look like
- init
{
[self sequentialISA];
[super init];
ivar1=...;
ivar2=...;
[self initIsMessagable];
[self doSomething];
[self moreThings];
[self initFinished];
return self;
}
Now, this might be overkill. C++ gives you sequentialISA and initFinished, but at the cost of single fixed constructors that are not true functions. [And, since C++ lets you specify ivar assignments on the constructor, it really also provides initIsMessagable].
Without any special compiler support, can an Objective C runtime support this, while still only having a single ISA pointer per object (for compatibility)?
The boring details:
sequentialISA, or initSequentialISA, would indicate that the object (at least at this class) wants to use this system.
initIsMessagable would guarantee that the ISA pointer is at the current class.
initFinished would mean that the ISA pointer can be moved down to the next lower class that is using this system.
The interesting questions:
How would this mix with init's that do not use this system? What if a leaf is just using the old, while a parent class is using the new? Since the last initFinished would be done by a parent, the last initFinished (identifiable as the first initSequentialISA) would be the final ISA resetter, and would set the message dispatch ISA to the object's ISA. With this model, initIsMessagable would always set the message dispatch ISA to the class of the method that the call was made from, and initFinished would set the ISA to the class of the method with the next-outer initSequentialISA.
But this needs two ISA, one for message dispatching, and another to identify what it is after all of the init's are done. And that breaks compatibility with NSObject's one ISA.
Can this be done compatibly?
--
I am a Mac 10-Cocoa/WOF/EOF developer, and I'm available for hire. Please contact me at michael-job @ stb.nccom.com if interested.
_______________________________________________
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.