gnustep-dev
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: ObjectiveC accessors


From: Stefan Urbanek
Subject: Re: ObjectiveC accessors
Date: Sun, 30 May 2004 12:21:08 +0200

On 2004-05-30 12:04:19 +0200 David Ayers <address@hidden> wrote:

Stefan Urbanek wrote:

<snip>

I was thinking about some decent inclusion of accessors in the ObjectiveC while maintaining language simplicity. After many attempts, through various @accessor and @getter I came with following simple and extensible language syntax:

@interface MyClass:NSObject
{
      NSString *name : public retained;
      NSArray  *collection : public readonly;
      NSString *weakReference : public nonretained;
}
- someInstanceMethod;
@end

So the syntax is:
@interface ...
{
      <ivar> : <keyword list>
}

where keyword list is space separated list of ivar attributes. The keywords used are just examples, feel free to imagine others that can be more appropriate. Meaning of the above keywords is: "public" - automaticaly generate getter and setter methods for the ivar -[MyClass ivarName] and -[MyClass setIvarName:]
"retained" - use [ivar retain] in the setter method
"nonretained" - do not use [ivar retain] in the setter method (one of the two retained and nonretained should be default)
"readonly" - do not generate the setter method

<snip>

Well actually the "retain/release/autorelease" paradigm is outside of the scope of the ObjC language. It's a part of the OpenStep specification and it's derivatives. There are however various macro based approaches to this issue. Maybe a semi "standardized" set of such macros could be a future feature of OpenStep derivatives.


Currently I am using:

#define GETTER(type, attr, capattr) \
    - (type)attr { return attr; }

#define SETTER(type, attr, capattr) \
    - (void)set##capattr:(type)value { attr = value; }

#define ACCESSORS(type, attr, capattr) \
    GETTER(type, attr, capattr) \
    SETTER(type, attr, capattr) \

#define RGETTER(type, attr, capattr) \
    - (type)attr { return attr; }

#define RSETTER(type, attr, capattr) \
    - (void)set##capattr:(type)value { ASSIGN(attr,value); }

#define RACCESSORS(type, attr, capattr) \
    RGETTER(type, attr, capattr) \
    RSETTER(type, attr, capattr)

and apply it as:

    RACCESSORS(NSString *, rootObjectNames, RootObjectNames)
    RACCESSORS(NSArray *, prototypes, Prototypes);
    RACCESSORS(NSDictionary *, objects, Objects);

However, I still need to write @interface method entries for both, getter and 
setter, so I have to write it in: ivar list, @interface method list, and 
@implementaion. And this is what I would like to avoid: to write single pattern 
on three places.

Speaking about retain/release being OpenStep specific. Why not to include some 
kind of garbage collection into the language too? It is used in majority of 
sources. Of course, with appropriate library hooks/callbacks, so one can 
implement custom GC method. I think it can be done in some simple way.

Stefan
--
http://stefan.agentfarms.net

First they ignore you, then they laugh at you, then they fight you, then you 
win.
- Mahatma Gandhi






reply via email to

[Prev in Thread] Current Thread [Next in Thread]