Em 2012-09-03 13:16, Derick Rethans escreveu:
I think the first thing anyone who uses generators must understand is that
they are iterators.

You forget that not even everybody that uses PHP knows what an iterator
is. For all they care, they have an array they can run foreach() on.

They should be compared with iterators, not arrays.

They shouldn't be compared with either. They are magical things that
happen with a "yield" keyword.

OK, you prefer to replace the facts (generators are iterator objects and they are introduced as such) with an alternative reality that putatively exists (or will exist) in the users' mind. I have no interest in a discussion in those terms, especially (1) without any evidence indicating such mental model is widespread and (2) given that the exceptions you claim to be surprising don't happen but for a programming error (i.e. they're not exceptions that need to be caught).

With that in mind, the behavior is not surprising for anyone who knows
how iterators and foreach interact.

More importantly, there is no other satisfactory solution (except a fatal error). foreach has no return value, so it has no other way to signal a failure. If we used a notice or a warning here what would happen is that code that used generators with an invalid state would, except for the notice, work as if it had been given an empty iterator. Put another way, it would fail with
only a notice, and continue.

Continue doing what?


I've replied here: http://www.mail-archive.com/internals@lists.php.net/msg60706.html

--
Gustavo Lopes

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to