Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:105218 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 57071 invoked from network); 10 Apr 2019 21:04:24 -0000 Received: from unknown (HELO localhost.localdomain) (76.75.200.58) by pb1.pair.com with SMTP; 10 Apr 2019 21:04:24 -0000 To: internals@lists.php.net References: <5cadfed8.1c69fb81.31f7d.1c49SMTPIN_ADDED_MISSING@mx.google.com> <5cae100d.1c69fb81.548c.48a4SMTPIN_ADDED_MISSING@mx.google.com> <5cae1fec.1c69fb81.d2407.c20eSMTPIN_ADDED_MISSING@mx.google.com> Date: Wed, 10 Apr 2019 19:01:35 +0100 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:60.0) Gecko/20100101 Thunderbird/60.6.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-GB Content-Transfer-Encoding: 7bit X-Posted-By: 94.0.205.114 Subject: Re: [PHP-DEV] [RFC] Nullable Casting From: markyr@gmail.com (Mark Randall) Message-ID: On 10/04/2019 18:34, Benjamin Morel wrote: > So why would you have different semantics for implicit `(?int)` cast vs `: > ?int` function return type cast, if they're both *out*? > Return type cast has the same semantics as parameter type cast. I would have to disagree with this as I think of "return" as a language construct function that takes one argument. When a return type is specified, that return function effectively inherits that type for its only argument, and limitations on implicit conversion should apply. > This looks weird to me. I would expect this last line to throw anyway. > Having "foo" passed somehow where your code expects an int(ish) or null, > should not be silenced and converted to NULL IMO. > To me, this last line just says "ignore any error here" - a bit like > prefixing it with @. That's exactly what it is, and thanks to null coalescence, you then have an easy available ability to either check if it succeeded, or default to another value. $items = (?int)$_GET['items']; if ($items === null) { // it didn't just resort to 0, which might be perfectly valid throw new Exception('Unsure how many items'); } $value = (?string)$_GET['name'] ?? ''; if ($value === '') { // also, ?name[]= can sod off throw new Exception('You must provide a name'); } -- Mark Randall