Syntax for simple Objective-C data structures

Originator:bigzaphod
Number:rdar://11605841 Date Originated:June 6, 2012
Status:Open Resolved:
Product:Developer Tools Product Version:
Classification:Enhancement Reproducible:
 
There are times when I want to make little data structure objects (often for intermediate use) to hold some kind of state or whatnot. Often I end up just using a dictionary - and while there's nothing terribly wrong with that, it's sometimes annoying to give up the type-checking, auto-completion, and other benefits of a real object with real properties. I'm wondering if there could be a middle ground that isn't so syntactically heavy as a "real" class but still gets the job done.

My suggestion is a simple syntactic sugar for creating property-only classes modeled a bit after C's structs. Here's a short example using the current syntax (with ARC) vs. my proposed @record syntax to accomplish the same thing more compactly:


@interface RangeRect : NSObject
@property (assign) CGRect rect;
@property (assign) NSRange range;
@property (copy) NSString *text;
@end

@implementation RangeRect
@synthesize rect, range, text;
@end



@record RangeRect : NSObject
    (assign) CGRect rect;
    (assign) NSRange range;
    (copy)   NSString *text;
@end

I think this would make the task of using small, internal data structures in Objective-C a bit nicer. @record would just create a normal class internally, so nothing too deep would be expected to change - this would just primarily be sugar. Although I could imagine expanding on this so that the scope of the generated classes could somehow be controlled so you could, for example, define a named @record in one implementation and in another implementation, you could define a different @record with the same name that's only used internally without them conflicting.

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!