Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Tentative coroutine syntax
#define coroutine(ARGS) \
__block unsigned long ext_coroutine_line_ = 0; \
\
return ^(ARGS){ \
for (;; ext_coroutine_line_ = 0) \
switch (ext_coroutine_line_) \
default:
#define return_coroutine \
}
#define yield \
if ((ext_coroutine_line_ = __LINE__) == 0) \
case __LINE__: \
; \
else \
return
id myCoroutine =
^{
__block int myVariable = 5;
coroutine(int n) {
// do stuff
yield 5 + n;
// do more stuff
yield ++myVariable;
}
return_coroutine;
}();
@jiangyewen

This comment has been minimized.

Copy link

@jiangyewen jiangyewen commented Sep 30, 2016

this is very tricky And obscure...
I was trying to expand the macros, only to find it was more confusing...
//===before expand

__block int i;

    int (^myCoroutine)(void) = coroutine(void)({
        for (i = 0;i < 3;++i) {
            NSLog(@"%@",@(i));
            yield i;
        }
    });

//after expanding

int (^myCoroutine)(void) =  ^{ 
     __block unsigned long ext_coroutine_line_ = 0; 

    ^(void) { 
        for (;; ext_coroutine_line_ = 0) 
            switch (ext_coroutine_line_) 
                default: 
                for (i = 0;i < 3;++i) {
                    if ((ext_coroutine_line_ = __LINE__) == 0)
                        case __LINE__: 
                    ; 
                    else 
                        return i;
        } 
            } 
    }()


  ({
        for (i = 0;i < 3;++i) {
            if ((ext_coroutine_line_ = __LINE__) == 0)
                case __LINE__: 
                ; 
            else 
                return i;
        }
    });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment