> -----Original Message----- > From: Nikita Popov [mailto:nikita....@gmail.com] > Sent: 20 August 2012 14:12 > To: Ángel González > Cc: Rasmus Lerdorf; Stas Malyshev; Derick Rethans; PHP internals > Subject: Re: [PHP-DEV] [RFC] Generators > > On Mon, Aug 20, 2012 at 1:56 PM, Ángel González > <keis...@gmail.com> wrote: > > On 20/08/12 02:01, Rasmus Lerdorf wrote: > >> I would still like to understand what this generator keyword would > >> actually do. I don't see how it would work. Would a > function marked > >> generator somehow not be allowed to return normally or to > finish and > >> not return anything? How could this be enforced? I am completely > >> against any keyword that is essentially documentation-only. > >> > >> -Rasmus > > Given that such function could "return several times", seems a > > different enough function type to have its keyword. > The method signature defines the API, so the caller knows what to use. > Can we agree on that? In this case it makes absolutely no > difference to the caller whether the function is implemented > using a generator, or whether it returns a custom Iterator > object. The "generator" > keyword wouldn't document the API, it would document an > implementation-detail. > > What would *actually* make sense here are return value typehints. E.g. > one could have something like `public Iterator getIterator() { ... }`. > This would provide the caller with information on what the > function returns, but would leave out the implementation > detail that the Iterator was implemented using a generator. > Or, if the generator implementation is actually important > (because it is used as a > coroutine) one could also explicitly typehint against it: > `public Generator getCoroutine() { ... }`. >
Yes. Quick example... interface A { function gen(); } class AImplementation implements A { function gen() { for($i = 0; $i < 10; ++$i) yield $i; } } class ADecorator implements A { private $a; function __construct(A $a) { $this->a = $a; } function gen() { return $this->a->gen(); } } $a = new ADecorator(new AImplementation()); foreach($a->gen() as $v) echo $v, "\n"; Obviously, AImplementation::gen() is a generator, but ADecorator::gen() isn't, but would want both to adhere to the same interface. So only return type hinting makes sense. > But return-value type hints are not directly related to generators. > They are a more general concept. If that's what all of you > want, then I'd recommend opening up a new thread for it. > > > You could not decorate it and rely instead on the presence of the > > yield keyword, but parsers will thank knowing about it from > the start > > rather than realising at mid-parsing that the function is a > completely > > different beast. > No, parsers don't care about this. It's trivial to detect in > both cases. > > Nikita > > -- > PHP Internals - PHP Runtime Development Mailing List To > unsubscribe, visit: http://www.php.net/unsub.php > Jared -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php