Giter Club home page Giter Club logo

objcplus's Introduction

ObjCPlus

"Extensions" for Objective-C - Coroutines, User Literals, Autoboxing

Overview

I was recently reading about an interesting way to extend the literal syntax and autoboxing of Objective-C. The article, OCUDL In Depth by Dustin Bachrach got me thinking. I enjoy the clarity and succinctness of the standard literal syntax but like Dustin am disappointed with its completeness (or lack thereof).

I have also been intrigued by the addition of User Defined Literals in the latest C++11. Adopting Objective-C++ to pull this in can be interesting as we will see in a bit. But before diving into C++ we should consider another option.

Clang now supports overloaded functions in C. Its not automatic but is made available with the addtion of a llvm annotation.

Literal Syntax

Autoboxing

int i = 23;
char *cstr = "A c-string";

NSNumber *num = @(i);
NSString *str = @(cstr);

But annoyingly, autoboxing an id or other ObjC instance results in an exception. It also only works on basic literals and is not extensible.

The standard system falls short on a number of points.

  • You can't box Objective C objects - trying to do so raises an exception.
  • Nothing can be done with nil / Nil
  • Structures and supported
  • It isn't user extensible.

see http://clang.llvm.org/docs/ObjectiveCLiterals.html

Autoboxing - new and improved

NSRect rect = NSZeroRect;
NSValue *value = $(rect);

NSValue* __attribute__((overloadable)) box(NSRect rect) { 
	return [NSValue valueWithRect:rect];
}

Literals++

Using the C++ Literal Syntax

http://www.preney.ca/paul/archives/636

inline NSURL *operator "" _url(const char *url_s, std::size_t len) {
    return [NSURL URLWithString:@(url_s)];
}

Coroutines

-(void)alert
{
    id result;
    BEGIN_COROUTINE();
    
    [[[UIAlertView alloc] initWithTitle:@"Alert One"
                                message:@"This is a message"
                               delegate:self
                      cancelButtonTitle:@"Cancel"
                      otherButtonTitles:@"OK", nil]
     show];
    
    YIELD();
    result = RETURN_VALUE;
    
    NSLog(@"Alert returns %@", result);
    
    [[[UIAlertView alloc] initWithTitle:@"Alert Two"
                                message:@"This is a message"
                               delegate:self
                      cancelButtonTitle:@"Cancel"
                      otherButtonTitles:@"OK", nil]
     show];
    
    YIELD();
    result = RETURN_VALUE;
    
    NSLog(@"Alert returns %@", result);
    
    END_COROUTINE();
}

- (void)alertView:(UIAlertView *)alertView 		didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    [self resumeCoroutineForMethod:@selector(alert) 		returnValue:@(buttonIndex)];
}

objcplus's People

Stargazers

PhilCai avatar Jiang Huan avatar Copper Cash avatar Docee avatar Alex Lee avatar  avatar

Watchers

Alex Lee avatar Jiang Huan avatar Lion avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.