> Am 03.03.2015 um 12:34 schrieb Rowan Collins <rowan.coll...@gmail.com>: > > Niklas Keller wrote on 03/03/2015 10:55: >> >> Gr, top-posting... >> >> >> Sorry, was on mobile. ;-) >> >> However, since the existence of the word "yield" is the only thing >> that >> marks a coroutine now, how about using a variant of that for the final >> value, e.g. "yield final $foo"? >> >> >> What's the final value? The last "yield"ed value or a return? > > "yield final" would mark the final result of the coroutine, as opposed to the > intermediate values passed out with a normal "yield". > > >> Just to give you some real world example: >> If you're using "return", it would look like that: >> >> public function getSession ($sessionId) { >> $result = yield $this->redis->get("session.{$sessionId}")); // We're >> waiting here until redis responded. >> return json_decode($result); >> } > > My suggestion is simply to change the keyword: > > public function getSession ($sessionId) { > $result = yield $this->redis->get("session.{$sessionId}")); // We're > waiting here until redis responded. > yield final json_decode($result); > } > > > The reasoning being that when you run getSession(42), it *doesn't* return the > result of json_decode(), it returns a pointer to the coroutine's state, which > you can resume later. > > Actually, I don't think that example makes sense, because JSON gets sent out > at the first yield, and then sent back in, so the caller would look something > like this: > > $foo = getSession(42); > $json_data = $foo->current(); > $foo->send($json_data); > $decoded = $foo->getReturn(); > > But never mind, I think we both get the idea. > > I understand the desire for a "final result", but I don't like reusing the > word "return", because it's never "returned" as the result of running the > function, it's just made available through some specific method/syntax. > > Regards, > -- > Rowan Collins > [IMSoP]
Hey, We currently already have "return;" (without value) as allowed syntax to terminate Generator execution. Thus, the logical consequence is just adding a value to that return. Also… if you "yield final" a value… then it logically would go into the generator resolving function as it's just like a normal yield (it's a last one, but still a yield). Would be weird to yield a value as explicit return value. When we want to *return*, it should be also a real *return". Yes, it is just accessible through special syntax (or a method), but it's still *return*ing into the calling frame. Why should the word "return" be unique to methods or functions? Bob -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php