php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #80735 yield break
Submitted: 2021-02-12 08:43 UTC Modified: 2021-02-12 18:39 UTC
From: petr dot vejchoda at seznam dot cz Assigned:
Status: Not a bug Package: Unknown/Other Function
PHP Version: 7.4.15 OS: Win10
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: petr dot vejchoda at seznam dot cz
New email:
PHP Version: OS:

 

 [2021-02-12 08:43 UTC] 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.


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-02-12 15:39 UTC] requinix@php.net
-Status: Open +Status: Feedback
 [2021-02-12 15:39 UTC] 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 17:38 UTC] petr dot vejchoda at seznam dot cz
-Status: Feedback +Status: Open
 [2021-02-12 17:38 UTC] 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 18:27 UTC] petr dot vejchoda at seznam dot cz
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.
 [2021-02-12 18:39 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2021-02-12 18:39 UTC] requinix@php.net
> yeah, well ArrayIterator([]) will create new array and then new object to
> basicly return nothing, right?
I wouldn't even use an iterator at all - I would just return [] directly. ArrayIterator would be more if you need an actual object. If you didn't then you could just return [].

> Ok the thing with those return statements inside generators is kind of confusing
> still to me.
If PHP sees a "yield" inside a function then it's made to return a Generator. Even if there's a "return" in there too, as long as there is at least one "yield" then it will be a generator function. And you can still return from it like any other function.

> I am using generators for coroutine implementation and that syntax would just
> look better.
Then you do need an actual object - either an iterator or a full Generator. Depends on exactly what you need to do with it.

> For me, having return statement in generator doesn't make sense.
Think of it as a "yield break".

Return values are also accessible with Generator::getReturn().
https://www.php.net/manual/en/generator.getreturn.php

Personally I haven't had much use for a return value, but being able to end the function/generator early with a return; is very handy for writing clean code.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Apr 29 05:01:28 2024 UTC