Req #81268 [Ver]: intersection type incompatible with nullable props/returns

From: Date: Fri, 16 Jul 2021 15:29:56 +0000
Subject: Req #81268 [Ver]: intersection type incompatible with nullable props/returns
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-235094@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=81268&edit=1 ID: 81268 Updated by: girgias@php.net Reported by: nicolasgrekas@php.net Summary: intersection type incompatible with nullable props/returns Status: Verified Type: Feature/Change Request Package: Scripting Engine problem PHP Version: 8.1Git-2021-07-16 (Git) Assigned To: girgias Block user comment: N Private report: N New Comment: > https://3v4l.org/aURFN/rfc#vgit.master I would consider that I bug in all honesty, because I forgot about PHP's peculiar implicit null handling... which I have no clue if it works properly with variances checks Previous Comments: ------------------------------------------------------------------------ [2021-07-16 15:28:06] girgias@php.net I addressed this on the PR (https://github.com/php/php-src/pull/6799#issuecomment-804793443), and I don't think we should accept it, it is super ambiguous ------------------------------------------------------------------------ [2021-07-16 15:25:55] nicolasgrekas@php.net Note that nullable intersection types are possible, here is one https://3v4l.org/aURFN/rfc#vgit.master $f = function (X&Y $foo = null) {}; $r = new ReflectionParameter($f, 0); var_dump($r->getType()->allowsNull()); Looking at the discussion at https://externals.io/message/113712, this was mostly overlooked. I think we should allow ?X&Y as property, return and parameter types. ------------------------------------------------------------------------ [2021-07-16 15:17:47] nicolasgrekas@php.net > this limitation is mentioned in the RFC Actual no, it's not. That's also why I opened the bug report, in case this was overlooked. Thanks for checking the error message, that's very much needed. ------------------------------------------------------------------------ [2021-07-16 15:14:14] girgias@php.net An intersection type cannot be part of a union, and nullable types are a form of union types, this limitation is mentioned in the RFC, so you can't initialize the property to null, however the error message needs to be clearer so this is part bug/part feature request. I'll have a look at this next week. ------------------------------------------------------------------------ [2021-07-16 15:07:10] nicolasgrekas@php.net Description: ------------ This code yields "Default value for property of type X&Y may not be null. Use the nullable type ?X&Y to allow null default value": class Test { public X&Y $y = null; } Fine, let's add the suggested "?". But then I get "Parse error: syntax error, unexpected token "&", expecting variable". About returning null|intersection, should this be possible? class Test { function foo(): ?X&Y { return null; } } Right now it yields a syntax error. ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=81268&edit=1

« previous php.bugs (#235094) next »