Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:112178 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 92193 invoked from network); 4 Nov 2020 05:22:10 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 4 Nov 2020 05:22:10 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 3D6381804B3 for ; Tue, 3 Nov 2020 20:42:45 -0800 (PST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,HTML_MESSAGE, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from mail-lf1-f52.google.com (mail-lf1-f52.google.com [209.85.167.52]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Tue, 3 Nov 2020 20:42:44 -0800 (PST) Received: by mail-lf1-f52.google.com with SMTP id 141so25375256lfn.5 for ; Tue, 03 Nov 2020 20:42:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=8NNf3cOol/+LVuQx94Lr3b/DT3LjtXn309mRSasirgQ=; b=FMmJ/UrXTXflMYGb9X96oNu0b2oeLZdocKkr0zgZmbRUbFz6y/YWaQMgUtzKvUHKnc y9ROc/IqHTVRLRC89v4s/yML8yJEwGkTHthjyRFn/kgXhtalhzBNUOcxfT05TrDBRm7w w8hGB+OTGdOPOTro2CFtpVf6nrG0268ox0P/1cmkWa9Ug4WCJ6tS+9qBHonV6AoI93ou RgnffPtcnmXKjnjuNagQGQ7cP/CBu6R1aNXwrH8kAvUeWBjkRY4QIXVZMBT0W2ywdaJh Mpr0NBrWI1uuQQvlzn4l5ws9i84fCgNeLJX8p3ZY3/i9wBEdNNX51jgcHJTSlbY2k4dm /v5g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=8NNf3cOol/+LVuQx94Lr3b/DT3LjtXn309mRSasirgQ=; b=Vun0u6FkBRqbmkLaUnAfIMM0t5QHXI/kNYCSluAo0OPj+/oZNbyBlPvac5SDMOwoKJ h7FD3gkPpmesEDg38Aqk7M9C4Qi9e/8uzOZPnYukrwfdU6cdsgOYfYWLJyayVNGJcFsn U9pGl9ZROKLJSjHtHsMPiTUG88Uf8+KAipLpcA+JWA5eTynF6tfQ1kt96kjm6Rpd+5cc YJXPNHWE9SOJnTsRXbGs2WNW2e+vHdcuJBN+BZB3xUAhzTscm7tlYY1hOciGEDmKwqYy ExBG2OD5seaoASJy5dPA18sMDyucy1Gfw1tkB/VBapK0D+TL1NKeph4YG/T9QOe/ChFm ejLw== X-Gm-Message-State: AOAM530P1rzkMXJB9sjPW7/a26eADtUiLW2sZEnP3McuUVGEQYjgMfF4 uKVFMoaKWtsBbZ1HG/WczmgC9S1SWU/Qb7N1Jj8= X-Google-Smtp-Source: ABdhPJwKU8aTUZzVpSQ+WBKTFLMV+WDgQJQUuvuMRoQ7oUey2axyEI3Fp+9cxU9vNmwIs8gUoAJXC+6L46LMZf1VHW4= X-Received: by 2002:a19:87d5:: with SMTP id j204mr9749041lfd.212.1604464962571; Tue, 03 Nov 2020 20:42:42 -0800 (PST) MIME-Version: 1.0 References: <02527028-FDC9-4CE8-869B-8EDBFABE6DEA@gmail.com> In-Reply-To: <02527028-FDC9-4CE8-869B-8EDBFABE6DEA@gmail.com> Date: Wed, 4 Nov 2020 06:42:31 +0200 Message-ID: To: Claude Pache Cc: PHP Internals Content-Type: multipart/alternative; boundary="0000000000002611b805b3409cbb" Subject: Re: [PHP-DEV] Nullsafe From: zsidelnik@gmail.com (Eugene Sidelnyk) --0000000000002611b805b3409cbb Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable However, `null` value is not a good way to use a default value instead of an object. When it comes to polymorphism, we would like to avoid null checks from being all around. The same goes not only for `->`, but also return / argument type-hintings. Why do we even need to know that object can be null? Null is just a special case (there may be other special cases). Does my customer code need to know about all special cases? Apparently not. But still, we create another operator to handle this case. It is ok when it comes to third-party libraries we don't have control over and sometimes it could return null. Yet this approach doesn't solve the primary problem. Polymorphism. You can argue that `null` can be used to indicate that something was not found. But this is not the case for returning null. Here an exception should be thrown. When I would like to return some default value of an object, it can't just be null (because it will pile up the code with ? signs). I need to create a separate NullObject, which implements a particular interface used at runtime, and return it's instance. If an interface declares a returning object, then I have to write even more boilerplate code for those objects. It is all for such thing as the default "null" value for an object. IMHO, simple things should be done in a simple way. What I currently see is that to do a simple thing, I need to create `N` `null`-classes, whose methods will eventually return `""` or `0` or `0.0` or `false` or `[]` - generally speaking `null`. On Tue, Nov 3, 2020 at 9:56 PM Claude Pache wrote: > > > > Le 3 nov. 2020 =C3=A0 17:38, Eugene Sidelnyk a = =C3=A9crit : > > > > Hello, internals! > > I am wondering why don't we use ordinary `->` operator with safe null > > handling? Programmers will be more prone to return null values. And thu= s, > > in most of cases `?->` will replace `->`. > > Why do we need another operator, if we can implement null safe in curre= nt > > operator without BC breaks? > > Hi, > > In a parallel world where people never commit bugs, yes, this is > reasonable semantics. > > However, it happens more than once that a null value occurs by accident > where an object is expected by the programmer. In this case, I very much > prefer to have a Warning or an Error, typically handled by an error handl= er > which sends a bug report to the developer, then triggers an emergency sto= p. > That makes it much easier to locate and fix bugs. > > Of course, this debuggability facility have to be balanced with the > inconvenience to having to write more code in cases where a null value is > expected. Having to write =E2=80=9C?->=E2=80=9D instead of =E2=80=9C->=E2= =80=9D in those cases is a minimal > inconvenience. Here, I mean =E2=80=9Cminimal=E2=80=9D in a very literal w= ay: exactly one > character of difference. > > Also, there is the following benefit: When I write =E2=80=9C->=E2=80=9D, = I am stating that > the LHS value should never be null; when I write =E2=80=9C?->=E2=80=9D, I= am stating that I > expect that the LHS is sometimes null. Thus, my code is documented in a > very concise way (between zero and one character), which is an aid for an= y > future reader (including myself). > > Aside: The name =E2=80=9Cnullsafe=E2=80=9D is misleading. The operator is= not about > safety, it is about handling null values in a concise way. In fact, in so= me > circumstances, sweeping an unexpected null value under the rug may be > totally unsafe. > > =E2=80=94Claude --0000000000002611b805b3409cbb--