Re: Categories and adding instance variables
Re: Categories and adding instance variables
- Subject: Re: Categories and adding instance variables
- From: Kay Roepke <email@hidden>
- Date: Sun, 1 Aug 2004 23:45:57 +0200
On 1. Aug 2004, at 21:59 Uhr, Dennis C. De Mars wrote:
The experts can address this more thoroughly, but I believe it is
because there may be code that did not see the category, possibly
compiled before you defined the category, and that code will allocate
memory for the instance variables without allocating space for your
new instance variable. Initialization is an issue also.
Aah, yes...adding methods probably just messes with some tables used to
look up the function pointers. But I was under the
impression that there was something called ivar lists. At least my
memory tries to convince me that I have seen such a thing in
some objc headers. I'll check.
I can't quite figure out from this description what the problem is.
Does Parser also derive from scanner, so you want to put this enum in
Scanner so both Lexer and Parser will share it?
I messed it up. Here it is again:
There are two classes Scanner and Parser (not the class I originally
called Parser...). They are providing methods common
to all specific lexers and parser you can generate.
Now you specify a grammar for both the lexical analysis and the parsing
rules. Out of the lexer description the tokens are
extracted and put into an enum TokenTypes. This enum is specific for
the specified language. In C++-Mode of the lexer/parser-generator
this is done by putting it in a class.
The lexer for this language (let's call it LanguageLexer) inherits from
Scanner. The parser (LanguageParser) from Parser.
Both need access to the TokenTypes class/interface (type is depending
on target language either C++/Java).
Scanner and Parser do not share a common superclass, thus LanguageLexer
and LanguageParser are not related, too.
To further complicate matters the TokenTypes are potentially needed in
other places, too (In which way is totally up to
the programmer using LanguageLexer/LanguageParser, I cannot be sure).
Also, if you think you can solve this in Java with an interface, why
can't the same solution be applied in Obj C with a protocol? Obj C
protocols are basically the same thing as Java interfaces, aren't
they? You can't declare instance variables in interfaces, so I'm not
quite sure how you would solve the problem in Java.
Currently the Java version does something like this:
public interface TokenTypes {
int EOF = 1;
int NULL_TREE_LOOKAHEAD = 3;
}
And then for LanguageLexer:
public class LanguageLexer extends Scanner implements TokenTypes,
TokenStream
{
// some methods
}
LanguageParser looks similar. This works fine.
Unfortunately in Java you can include constants in interfaces, which
ObjC cannot do in protocols. A better way of putting
my problem would be categories and adding constants...well, anyway.
Does anyone have a suggestion how this could be cleanly done?
One thing I came up with would be to use a static dictionary and get
the tokens from there, but that
seems like overkill to me.
The static dictionary is pretty much the standard thing to do when you
want to add "instance variables" to a category. You create a private
static dictionary associated with that class, and arrange to have each
instance enter itself as a key of the dictionary, with the value being
the instance variable (or variables) you want to add. It is clearly
less efficient than having a real instance variable and takes some
effort to set up. so it is a matter of how badly you need the instance
variable.
Personally, I would just add the enum to each subclass. (I've become
more cavalier about issues of type safety and type identity since I've
been using Objective C). If you want to put a more formal veneer over
this, you could do the following:
That would surely work, thanks. But still there's the problem of making
the TokenTypes available to other classes, too.
I guess I'll have to look for a different approach.
Thanks,
Kay
_______________________________________________
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.