Bug #81330 [Com]: Generator does not allow return null with implicit yield
| From: | greedy dot ivan at gmail dot com | Date: | Wed, 04 Aug 2021 13:04:15 +0000 |
| Subject: | Bug #81330 [Com]: Generator does not allow return null with implicit yield | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-235596@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=81330&edit=1
ID: 81330
Comment by: greedy dot ivan at gmail dot com
Reported by: greedy dot ivan at gmail dot com
Summary: Generator does not allow return null with implicit
yield
Status: Not a bug
Type: Bug
Package: *General Issues
Operating System: All
PHP Version: 8.0.9
Assigned To: cmb
Block user comment: N
Private report: N
New Comment:
I understand it.
This is a refactoring issue. You have a return type hint. You have an yield inside a function. And
when you move yield instruction somewhere you get a runtime type error because you
generator-function now use return instead of yield. And you get this error not on a primary case.
I agree that it is not a bug. One more difficulties with generators in php.
Previous Comments:
------------------------------------------------------------------------
[2021-08-04 12:44:05] cmb@php.net
Actually, the point is that bar() is not a generator function at
all, because there is no yield. You probably want something like
<https://3v4l.org/hRSNS>.
------------------------------------------------------------------------
[2021-08-04 12:25:36] greedy dot ivan at gmail dot com
There are three options here.
1. There is an yield operator inside a function. Parser gets it and allow to return null as a valid
end for generator.
2. There is no yield operator inside a function but there is a return instruction that returns
Generator. This line works fine.
3. The same as 2, but we have a line, that returns null. It will throws Error, because Parser
doesn't allow return an empty Generator such way. It doesn't get, that function return
Generator at all, because there is no yield operation inside function itself.
So, we must use some tricks to return empty Generator in this case, or use
yield from
func() instead for return func().
------------------------------------------------------------------------
[2021-08-04 11:57:48] cmb@php.net
That type check is done at runtime, not compile time. And there
is no implicit yield.
------------------------------------------------------------------------
[2021-08-04 11:49:42] greedy dot ivan at gmail dot com
It doesn't throw error on all other cases.
It will throw Type error only in case, when yield is implicit and null is returned.
------------------------------------------------------------------------
[2021-08-04 11:39:32] cmb@php.net
If the generator function does not necessarily return a Generator,
you shouldn't declare it as such.
------------------------------------------------------------------------
The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
https://bugs.php.net/bug.php?id=81330
--
Edit this bug report at https://bugs.php.net/bug.php?id=81330&edit=1