Req #80735 [Com]: yield break

From: Date: Fri, 12 Feb 2021 18:27:10 +0000
Subject: Req #80735 [Com]: yield break
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-232106@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 Comment by: petr dot vejchoda at seznam dot cz Reported by: petr dot vejchoda at seznam dot cz Summary: yield break Status: Open 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: Also I would like to point out, that I am using generators for coroutine implementation and that syntax would just look better. I don't care much, that you think it doesn't make sense. It does in my point of view. For me, having return statement in generator doesn't make sense. Well ... have a nice day. Previous Comments: ------------------------------------------------------------------------ [2021-02-12 17:38:26] petr dot vejchoda at seznam dot cz yeah, well ArrayIterator([]) will create new array and then new object to basicly return nothing, right? Ok the thing with those return statements inside generators is kind of confusing still to me. However ok. It would be nice to have static/const object of empty iterator, just so you don't have to create instance every time. But yeah, I will just make it in utilities class I guess. ------------------------------------------------------------------------ [2021-02-12 15:39:43] requinix@php.net 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([]). ------------------------------------------------------------------------ [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

« previous php.bugs (#232106) next »