Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:115134 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 65361 invoked from network); 25 Jun 2021 06:39:31 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 25 Jun 2021 06:39:31 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 95C2A1804C3 for ; Thu, 24 Jun 2021 23:58:28 -0700 (PDT) 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-yb1-f176.google.com (mail-yb1-f176.google.com [209.85.219.176]) (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 ; Thu, 24 Jun 2021 23:58:28 -0700 (PDT) Received: by mail-yb1-f176.google.com with SMTP id b64so3137339yba.0 for ; Thu, 24 Jun 2021 23:58:28 -0700 (PDT) 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; bh=ooamSk2mebUZjuGjdj8Ih0aQonU+SPabyJhLoJsFLFg=; b=o/24+tmmqo9YPGVnYvtW0fJi3I9Y0pAudqTzJabaLy+BBQX279cZT/BqWE+dUmmCUf 6Ld5M2b4NluFAMLU7CTSLA47u5hr7g0yIC1/hmanF0/aU9wU8mV/SQ8EXNp3Jy3RAbuk ZKzkxnGozGFIIYRE7vCrhJL+NAD/n7rdfiLG/p8GTF0ZLbKKUHWVtEFi9fTPoT1Nw5GJ IYthVsO0CiwVY+44Gh/+bjQSJQzGzH7Dh1m1dAI2KKQt2a5Z/OE2mEzYHLDYvSK92SLU nhjIMv5nKQ0gFHVV218tnHghj3GbGu4Fsl9mYmeTf0tYcG/PmVp1wXOgOzt6HIdoAZnM WoXA== 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; bh=ooamSk2mebUZjuGjdj8Ih0aQonU+SPabyJhLoJsFLFg=; b=fUakQa/goajkMb1TfH8txDlhRDf+J+RZsYeAUX7ROI6WqKRWbrQUZa5jS5NxBSe9PT +Vu6YqZGkOJWoLVCIZ/toEe3JaZ7bUg8xGOS8LErR4rczUBVR5hwisuMWi/thYF2SnuD BWq19RAuKCfiiqqOMG0yz8XZJfC7PM11615Jut8Uoi4FDdrkUQNIoxxISyYPu763iIyk 79LEaqVx/DAThOoPhcXzCsley+KVcg94Es2NvibNSdkh8tR34+pHsW6+KyEllXZKDRf/ /F3srRb7iGJJSHIgOCgF+xhOGOL3ac0mIRmIwLZEj2oBQAoo3pmUqt1bAfve4vRPjmhF TXDA== X-Gm-Message-State: AOAM531qebKz58dKoFV/gKtyKG5Sx6ZNaEQ/F5371BB1pbO4m5CD+fjP HYvrzawGfiqEpd0tailCywf/Z+R7GXXvfg4iHV6635a9xwU= X-Google-Smtp-Source: ABdhPJwbTD3efmj4iU7hRXD16kL3u4jXpkR+mrAaVitR3XcWwDtMCq6uLTU8XftTaSpdk+LaSO63LaOQIN583mcBwGQ= X-Received: by 2002:a25:d913:: with SMTP id q19mr10635102ybg.397.1624604305151; Thu, 24 Jun 2021 23:58:25 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Fri, 25 Jun 2021 09:58:17 +0300 Message-ID: To: PHP Internals Content-Type: multipart/alternative; boundary="00000000000082577605c591aa28" Subject: Re: Nullsafe Types From: zsidelnik@gmail.com (Eugene Sidelnyk) --00000000000082577605c591aa28 Content-Type: text/plain; charset="UTF-8" I would really like to hear your thoughts on this. On Thu, Jun 24, 2021, 10:14 PM Eugene Sidelnyk wrote: > Hello, everybody! > > After using nullsafe operator for a while, I have some further thoughts. > In most cases when we make type nullable, we would use nullsafe operator > if we don't know how to deal with it. This causes clogging main code with > this ? signs. Possible solution is to make nullsafe type in the first place > rather than duplicate nullsafe operator everywhere. > > > The idea is pretty simple. > > Instead of doing this: > > ```php > > class Foo > { > public ?Bar $bar = null; > } > > class Bar > { > public int $baz; > } > > fas($foo->bar?->baz); > tes($foo->bar?->baz); > > ``` > > We can remove duplication of question mark in all usage places by making > type nullsafe. > > ```php > class Foo > { > public ??Bar $bar = null; > } > > class Bar > { > public int $baz; > } > > fas($foo->bar->baz); > tes($foo->bar->baz); > > ``` > > Regarding union types: > > ```php > class Foo > { > public ?null|Bar $bar; > > public Baz|Tres|?null $res; > } > ``` > > In first case, `?null|Bar` is long form of `??Bar`. > In second one, `Baz|Tres|?null` value of this type can be either Baz or > Tres or "safe null". Safe null is basically an pseudo-object, which for any > interaction (property acces or method call) returns same "safe null". > > > For union types, safe null should only be allowed in disjunction with > object. > > ```php > class Foo > { > // Error > public ??int $int; > > // allowed > public ??object $o; > > // allowed, at least one object > public ?null|Bar|int|array $val; > } > ``` > > Special attention requires nullsafe mixed value. It doesn't require two > question marks, but one, because mixed itself has an null inside. > > ```php > class Bar > { > // this is nullsafe mixed value > public ?mixed $mixed; > } > > $bar->mixed->property; > ``` > > > Safe null must behave almost like null. So that $safeNull === null is > true. If we would really like to tell apart safe null from null, some > helper function may be introduced. > > > About method calls, this should be allowed: > > ```php > $resultOrSafeNull = $valueOrSafeNull->someMethod(); > ``` > > Using nullsafe operator on nullsafe value should be allowed: > > ```php > $valueOrSafeNull?->method(); > $valueOrSafeNull?->property; > ``` > > Nullsafe types should be allowed as: > - parameter types > - return types > - property types > > > > > --00000000000082577605c591aa28--