Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:92318 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 86703 invoked from network); 14 Apr 2016 21:20:00 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 14 Apr 2016 21:20:00 -0000 Authentication-Results: pb1.pair.com header.from=fsb@thefsb.org; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=fsb@thefsb.org; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain thefsb.org designates 173.203.187.75 as permitted sender) X-PHP-List-Original-Sender: fsb@thefsb.org X-Host-Fingerprint: 173.203.187.75 smtp75.iad3a.emailsrvr.com Linux 2.6 Received: from [173.203.187.75] ([173.203.187.75:49881] helo=smtp75.iad3a.emailsrvr.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 5A/F3-64966-D7900175 for ; Thu, 14 Apr 2016 17:19:59 -0400 Received: from smtp26.relay.iad3a.emailsrvr.com (localhost.localdomain [127.0.0.1]) by smtp26.relay.iad3a.emailsrvr.com (SMTP Server) with ESMTP id 1BCEA80372; Thu, 14 Apr 2016 17:19:55 -0400 (EDT) X-Auth-ID: fsb@thefsb.org Received: by smtp26.relay.iad3a.emailsrvr.com (Authenticated sender: fsb-AT-thefsb.org) with ESMTPSA id 96996801C5; Thu, 14 Apr 2016 17:19:53 -0400 (EDT) X-Sender-Id: fsb@thefsb.org Received: from [10.0.1.2] (c-66-30-62-12.hsd1.ma.comcast.net [66.30.62.12]) (using TLSv1 with cipher DES-CBC3-SHA) by 0.0.0.0:465 (trex/5.5.4); Thu, 14 Apr 2016 17:19:55 -0400 User-Agent: Microsoft-MacOutlook/14.6.2.160219 Date: Thu, 14 Apr 2016 17:19:51 -0400 To: Levi Morrison CC: php-internals Message-ID: Thread-Topic: [PHP-DEV] [RFC] Nullable Return Type Declaration References: In-Reply-To: Mime-version: 1.0 Content-type: text/plain; charset="UTF-8" Content-transfer-encoding: 7bit Subject: Re: [PHP-DEV] [RFC] Nullable Return Type Declaration From: fsb@thefsb.org (Tom Worster) On 4/14/16, 12:35 PM, "Levi Morrison" wrote: >I can appreciate that you want only the restricted union with null. >However, I do not see the point of disallowing it for parameter types >while allowing it for return types: > > function setLeft(Node $n = null) { > $this->left = $n; > $this->updateHeight(); > } > >Why disallow the explicit union with null here instead of the default >parameter which does not exactly capture the desired semantics? >Calling `$node->setLeft()` is just odd, almost as if it was a mistake. >I would much prefer `$node->setLeft(null)` here. Basically, if we have >a feature for return types that exactly matches the semantics that we >occasionally want for the parameter types why forbid it? I agree that `$node->setLeft()` is weird but I find `$node->setLeft(null)` still a bit weird. Perhaps something like `$node->resetLeft()` would work? Was that the idea? >Additionally, on occasion I'll see functions like this: > > function foo(Bar $b = null, $not_optional_param); The only thing we can know for sure from this is that the programmer urgently needs reeducation :) >Why not allow nullable types on parameters to avoid that wonkiness >caused by default values of null? > > function foo(Bar | Null $b, $not_optional_param); > >This is much better. Yes but still a code smell to me. I'd need to know more about the programmer's intent for `foo(null, "value")`. It might be better to swap order, or change the method name, or add another method... Who knows? Need to take each case individually. This kind of asking questions about intent in code review is good for code quality. That's why I like how PHP doesn't allow this. It encourages the question asking. Every case is different, of course, so you can surely find counter examples. But on balance I'd say it's better to disallow it. Does this help you understand my preference? I think the restriction encourages a healthy discipline. Otoh, I think nullable return is a pressing need. Tom