Req #79669 [Sus]: Nullable variables
| From: | requinix@php.net | Date: | Wed, 03 Jun 2020 17:55:23 +0000 |
| Subject: | Req #79669 [Sus]: Nullable variables | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-227301@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=79669&edit=1
ID: 79669
Updated by: requinix@php.net
Reported by: valentiny510 at gmail dot com
Summary: Nullable variables
Status: Suspended
Type: Feature/Change Request
Package: Variables related
Operating System: nix
PHP Version: Next Major Version
Block user comment: N
Private report: N
New Comment:
> $a = @$b; (btw this doesn't work either...)
Yes it does. https://3v4l.org/03cuu
For more complicated expressions you can promote the @ to the whole assignment:
> @$a = $b + $c;
Previous Comments:
------------------------------------------------------------------------
[2020-06-03 14:37:02] valentiny510 at gmail dot com
ups, typo...
instead of
$a |= $b;
I meant...
$a \= $b;
(my pinky didn't reached that far)
------------------------------------------------------------------------
[2020-06-03 14:30:29] valentiny510 at gmail dot com
To resume...
Yes, having the "Undefined variable: x" error is good, unless you Specifically
supress that error with a mechanism like, $a = @$b; (btw this doesn't work either...)
When I say I was looked at the RFC before, it was because I thought for a split second
that the equal sign changed the behaviour and NULLIFY the $b variable, but then
I was like "nah.. not possible", and of course is not...
The coalesce operator is Great, with the small exception that you
MUST have a FALLBACK value, EVEN IF that value is NULL...
What I requested is an operator that doesn't require any
FALLBACK and automatically NULLIFY the ASSIGMENT
There are plenty of options, aside from the ones I already mentioned,
maybe even we can reuse the "old" ternary/elvis operator like this
$a = $b ?:; without requiring to have the last value either
Some examples...
$a = ?$b; # like nullable types (but require to have at least one space in between...)
$a =? $b; # preferred operator (so the parser can ignore all the spaces...)
$a = @$b; # maybe supress the error and return null?
$a ?= $b; # one sign to not interfere with ??=
$a = $b ?:; # elvis2.0? but why not shorter...
$a = $b ?; # the parser can say "after ? if you find ; assign null"
$a |= $b; # weird but...
There are so many other symbols to choose from...
Is not that hard to understand what I want
------------------------------------------------------------------------
[2020-06-03 13:39:14] cmb@php.net
This is obviously controversial (and there may be alternative
solutions), so requires discussion for which this bug tracker is
not suitable. Instead, please send a mail to the internals
mailing list[1]. For the time being, I'm suspending this ticket.
Thanks.
[1] <https://www.php.net/mailing-lists.php#internals>
------------------------------------------------------------------------
[2020-06-03 12:05:56] valentiny510 at gmail dot com
The example you gave $array['key'] ??= computeDefault(); is pointless,
To ASSIGN a default value for $a can be achived "normally"
$a = $b ?? compute_default( );
I want the ASSIGNMENT of $a to be NULLABLE if $b is NOT SET
$a = $b;
var_dump($a); null (without any errors!)
So I don't want the MANUALLY write
$array['key'] ??= NULL;
COMPRENDE?
Is the Exact behaviour I was talking about...
Damn, you guys confused me for a bit
------------------------------------------------------------------------
[2020-06-03 12:02:37] bugreports at gmail dot com
> $a ??= $b;
> where $b doesn't exists, still throw the error "Undefined variable: $b"
which is a good thing, i haven't seen "Undefined variable" in code which should not
be fixed the last 15 years and you should ask yourself why $b is undefined here
------------------------------------------------------------------------
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=79669
--
Edit this bug report at https://bugs.php.net/bug.php?id=79669&edit=1