Skip to content

Instantly share code, notes, and snippets.

@jspahrsummers
Created July 4, 2011 03:03
Show Gist options
  • Save jspahrsummers/1062849 to your computer and use it in GitHub Desktop.
Save jspahrsummers/1062849 to your computer and use it in GitHub Desktop.
typedef id (^Tuple)(size_t);
#define tuple(...) \
tupleMake(__VA_ARGS__, nil)
#define emptyTuple \
tupleMake(nil)
Tuple tupleMake (id firstObject, ...) NS_REQUIRES_NIL_TERMINATION
{
if (!firstObject) {
Tuple t = ^ id (size_t n){
return nil;
};
return [[t copy] autorelease];
}
// blah blah get varargs here
size_t numberOfObjects;
id objects[numberOfObjects];
Tuple t = ^ id (size_t n){
if (n < numberOfObjects)
return objects[n];
else
return nil;
};
return [[t copy] autorelease];
}
// usage
Tuple t = tuple(@"foo", [NSNumber numberWithInt:1]);
NSString *str = t(0);
NSNumber *num = t(1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment