
On Fri, Sep 21, 2012 at 1:34 PM, Oliver Kowalke <oliver.kowalke@gmx.de> wrote:
template <typename C> void timer(C c, chrono::seconds interval) { // while( interval != chrono::seconds::zero() ) { // this_thread::sleep_for(interval); // interval = c(interval); // }
}
coroutine<chrono::seconds(chrono::seconds)> c( [](coroutine<chrono::seconds(chrono::seconds)> c, chrono::seconds i) { timer(c, i); } );
chrono::seconds s = c(chrono::seconds(1));
Which value has variable 's' after return from 'c(chrono::seconds(1))' ?
chrono::seconds(1) since timer() yields whatever was initially passed in and then whatever was returned by c().
in the modified example timer() never yields (c(interval) is not called) - it simple returns. no value was transfered from coroutine function to the caller -> coroutines must return the type of the signature
Ok, I understand now. Like Giovanni mentioned in his response, an exception can be used (just like your current implementation throws coroutine_terminated). There might be a better way, I don't know. I wouldn't want to use optional<> here as it would then not be inter-operable with a regular function. In case of generator<>, optional<T> is OK since generators are not meant to be symmetric. I must admit that during this exercise I was looking for desirable features but I didn't think the behavior all the way through. Regards, Eugene.