Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:115129 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 21851 invoked from network); 24 Jun 2021 18:56:01 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 24 Jun 2021 18:56:01 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 3B7C21804D8 for ; Thu, 24 Jun 2021 12:14:51 -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=-0.2 required=5.0 tests=BAYES_40,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-f179.google.com (mail-yb1-f179.google.com [209.85.219.179]) (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 12:14:50 -0700 (PDT) Received: by mail-yb1-f179.google.com with SMTP id m9so859501ybo.5 for ; Thu, 24 Jun 2021 12:14:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:from:date:message-id:subject:to; bh=jJVBw/xfL9ge5rIHwXuPYB/jiH4iOvFcsPwzkxSqXDc=; b=Y/QfIycKv+p+jAX3FrYOD//0RZ8mtsXhBURyExTcN1/ya12O5/YbCuehjTToB4G0G1 sWfB0A9hWyKFvu+TS3+37qdPgzsfIBjyRlbeQH4rYTbbcLgHDXct+NsOVLtxhUrHlQbp c1sMANA//f07CVh/75BIN9FW9rIvaAOTOjSL12wz0Qo0i6JPw3T/hJ0jdU78E6QrUH3t UoROnW/zd34AwsoaR3MwH8ByjCPTf1Gqx+r2mFzYEnnDeEOMFpAb/EO4GLAcw4bXgEDm Uzv9Jk7idagUCuURLw6Ri5PeplZL7uyiEs4mnIiyKjtBZ4TT7eBhSosm+GxEcIivNsWV 6FJQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=jJVBw/xfL9ge5rIHwXuPYB/jiH4iOvFcsPwzkxSqXDc=; b=VUnVot0X028vAuulF7ulZiiGh+PodpiIrMxSFXaBYI2HY8J/3Z9ZiqIsuUbmcBhJjI aw6uckQEi+Rcs7lqAKjhQmjVmdROu6eXOh/oT8cGBAqCDPvLhG5zokYFQtg8b4xsoZOj P5cxKW19FoEwgOVarXWa6N8mcqpqKL75i+/HNDn+nCfor8Jc4eq4mxmb0f02MazRGXtu nfLQiZSBGt5WqN3FGOXeY8eSVwvbfFV0W0X2rHhNpHm9IknWKWcPjSPbFbAKX+BArxL/ it7jqMSUUeoEUEE+N+xI0TKReiJ8nTxXIJAq/jjbOsT8Z8aKZJ2rpLbIWdYZFr6wm3+s KB7A== X-Gm-Message-State: AOAM532g1IOUijpg7mfs/PglKF7LQlKKoU6J4v3DfHfN8Z6mCepM1s4J cQWpYizGQ33wROrZsVyuiBx3d6LPn3PYv82bKQ3G3y90ImCpww== X-Google-Smtp-Source: ABdhPJyC+3MdpXhrxm649dVNx+kS8SseYzDx0R8wcmDXm/wlzJ7XW+cb+nBF8y5HsXhKeB3D2mCh9yOg4ff+hXItPsA= X-Received: by 2002:a25:502:: with SMTP id 2mr7577835ybf.498.1624562088318; Thu, 24 Jun 2021 12:14:48 -0700 (PDT) MIME-Version: 1.0 Date: Thu, 24 Jun 2021 22:14:37 +0300 Message-ID: To: PHP Internals Content-Type: multipart/alternative; boundary="00000000000030992d05c587d6af" Subject: Nullsafe Types From: zsidelnik@gmail.com (Eugene Sidelnyk) --00000000000030992d05c587d6af Content-Type: text/plain; charset="UTF-8" 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 --00000000000030992d05c587d6af--