Re: Categories and adding instance variables
Re: Categories and adding instance variables
- Subject: Re: Categories and adding instance variables
- From: Brent Gulanowski <email@hidden>
- Date: Sun, 1 Aug 2004 18:15:03 -0400
On Sun, 1 Aug 2004 19:27:27 +0200, Kay Roepke
<email@hidden> wrote:
>
Yes, I know it doesn't work. I know about subclassing.
>
>
Now that I said that ;-), here's my question:
>
>
Is there a technical reason that you cannot add instance variables to
>
class using categories?
>
Or is it just convenient for the runtime?
>
>
I have this generated class, lets call it Lexer. It inherits from
>
Scanner, which is part of an existing framework.
>
While generating Lexer's source code, I also end up with a enum
>
containing the tokens to be produced by Lexer.
>
>
Of course I could simply add this enum to the Lexer class and be done
>
with it, but guess what:
>
I have a Parser class which wants the very same enum, in order to know
>
which tokens it must
>
handle. Obviously, I don't want that enum to be duplicated in my
>
(albeit generated) code for
>
hygenical reasons.
>
In the C++ version this is done via multiple inheritance and in Java
>
one could simply make an
>
interface for this. ObjC on the other hand craps out.
>
>
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.
In many cases like this, you are often advised to remove the shared
functionality and make it into a completely different class, which the
original classes can delegate the work to -- hence, a delegate class.
Unfortunately your descriptions of your design problem are rather
polluted by issues of the techniques of interpreting. For examle, this
"enum" -- a C enumerated type or some kind of class? If an enumerated
type, I don't see how you can "add it to the Lexer class", so it must
be some kind of unique class, in which case I fail to see why both the
Lexer and Parser cannot simply hold a reference to it. Is "token" a
class or just a an instance of another class (like NSString)?
As for putting constants in an interface, you can do that just fine.
You can define any standard C enumerated type in any .h or .m file,
and you can define file variables in the category implementation file,
just as you can add such "global variables" in a C implementation
file. This seems to be the key point you have missed out on. For if
they are only constants, they are effectively like "class variables"
in Java, not instance variables at all.
Cheers,
--
Brent Gulanowski
http://www.boredastronaut.com
_______________________________________________
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.