Req #80735 [Opn->Fbk]: yield break
| From: | requinix@php.net | Date: | Fri, 12 Feb 2021 15:39:43 +0000 |
| Subject: | Req #80735 [Opn->Fbk]: yield break | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-232095@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=80735&edit=1
ID: 80735
Updated by: requinix@php.net
Reported by: petr dot vejchoda at seznam dot cz
Summary: yield break
-Status: Open
+Status: Feedback
Type: Feature/Change Request
Package: Unknown/Other Function
Operating System: Win10
PHP Version: 7.4.15
Block user comment: N
Private report: N
New Comment:
1. You can end a generator by return;ing.
2. An empty Generator from a function that doesn't even have code to yield anything
doesn't make sense. Just return an empty iterator, like ArrayIterator([]).
Previous Comments:
------------------------------------------------------------------------
[2021-02-12 08:43:35] petr dot vejchoda at seznam dot cz
Description:
------------
I was looking for a way to end a end a generator prematurely without necessity to make it to the end
of function/method. Also way of marking a function/method as an empty generator. And I found none.
If there is one, one can say I can't google properly, or one could also say it was not written
in the documentation.
Test script:
---------------
abstract class Base
{
__construct()
{
foreach($this->generator() as $key => $value)
{
$this->doSomething($key, $value);
}
}
protected abstract function generator(): traversable;
}
class Child extends Base
{
// empty generator??
protected function generator()
{
if (false)
{
yield null;
}
}
}
Expected result:
----------------
I would appreciate some language construct like yield break; that would end the generator
prematurely, or could be used to simplify empty generator syntax.
yield from new EmptyIterator(); is not really an option for me, sorry, thats just plain ugly.
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=80735&edit=1