Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:115494 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 20324 invoked from network); 19 Jul 2021 14:01:47 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 19 Jul 2021 14:01:47 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 1B81F180533 for ; Mon, 19 Jul 2021 07:26:48 -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-ed1-f49.google.com (mail-ed1-f49.google.com [209.85.208.49]) (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 ; Mon, 19 Jul 2021 07:26:47 -0700 (PDT) Received: by mail-ed1-f49.google.com with SMTP id ca14so24265993edb.2 for ; Mon, 19 Jul 2021 07:26:47 -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 :cc; bh=aaallnNFMtwCbpCXMt8HwdEhdHIBxSYDJ6oeqS21ZvA=; b=Je7+9BmvJYkBI/8VWDv+APrPCt2nA4OgiCgZjJpKDSLjI9yIGRsH8siqhR+wKIAn6w OZVVh1J7y3sItgC5V1zR2xS3mNaA71ZKqxeX+0TPCtsoAPYwYwbthlTjXu64XSTt9lYJ XfR2C5N89o/O8nLrhNupaO0e3MALcvwkcEtrWC9oioR8KGWrSZcIPqR2xgQa4nVXGN0L 7y86aw2dL6NQvbgnikHbppCrdjmePJCiBZK5jIfHdk2L+OQSn/bZ7MxDLIh6B/OrVD2l swFyiXGz67CIgssrYVHop/OpiiKR0WXvBJjit2Tn9wdnCZZh2ehVDYYA3cvewRwawiG8 x/3Q== 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=aaallnNFMtwCbpCXMt8HwdEhdHIBxSYDJ6oeqS21ZvA=; b=qleDV5A34bfSbjhWWLuV9ru5/rzCEqdHw0OOKNsAyX48eorOwXz75d1IuTFRrNORNs yR0e3aOrzEfb27NvcXQG/X3bGSqlM71Ul6rkS66G0pBb4s1r0lhUkeAxqZ15jC2QQuht mvC6afwGAQRmtbvl/RWzgN2vGFCuuhWHfcg14OpZgwnAbCbjtk4bOStTOsWcqJAGXKx4 WZNTo3V9rAY+7N4aQak5xaR65YsSn8mwohtSDTGy4LNkfDA/6cLfGJMtMT+iYydEKeDj 48pb2XsIwy3qbbTuA5qrAIA0lmBcTLq5wzHsSOmb67k0QHCb0Py2PWpff63uFR07cYVd wlJw== X-Gm-Message-State: AOAM53379zaKftxn5BRToKD9ySfmfbgo+kttMZIek2pnbGwFfkiwVyqI YuStRtr+W8eKfKmu1fzKX5rc1Es/EcohrC6+d/k= X-Google-Smtp-Source: ABdhPJx6uRsdxPBCTxZ8BXYzEjQwA1P7JOIfqDvTyVK5QGJKAPBfmYdhxk3m/0Gfr52wOAJnC8Cs9r0OWndy9UrDkhM= X-Received: by 2002:a05:6402:c17:: with SMTP id co23mr35211410edb.68.1626704806358; Mon, 19 Jul 2021 07:26:46 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Mon, 19 Jul 2021 16:26:34 +0200 Message-ID: To: Guilliam Xavier Cc: PHP Internals List Content-Type: multipart/alternative; boundary="00000000000023326905c77aba64" Subject: Re: [PHP-DEV] intersection types and null for defaults, properties and return types From: nicolas.grekas@gmail.com (Nicolas Grekas) --00000000000023326905c77aba64 Content-Type: text/plain; charset="UTF-8" > I want to bring your attention to a behavior that was mostly overlooked: >> >> 1. it is not possible to use an intersection type with an argument that >> defaults to null >> 2. it is not possible to use an intersection type with a nullable >> property (nor to make it default to null) >> 3. it is not possible to use an intersection type with a nullable >> return >> type >> >> Actually, 2. was possible until it was "closed" in >> https://github.com/php/php-src/pull/7254 >> >> I reported these behavior and you might find some discussion about it in >> https://bugs.php.net/81268 >> >> Looking at the past discussion on this list ( >> https://externals.io/message/113712) and at the rfc itself ( >> https://wiki.php.net/rfc/pure-intersection-types), this was mostly >> overlooked. >> >> That's why I'm posting this message. So that we can have that missing >> discussion here. >> >> To me, we are going to need (userland "we") these capabilities. >> >> It's quite surprising to be "forced" to return something, or "forced" to >> pass a value, when all other types in PHP allow "null". I know about the >> null pattern, but it is quite uncommon in PHP, because "null" works just >> great usually. >> >> I feel like we "just" need to agree on a syntax to make this possible. It >> was first suggested in the related PR to use "?A&B" (see >> https://github.com/php/php-src/pull/6799#issuecomment-804761117) >> >> This was rejected by the author with the reasoning that (?A)&B could mean >> (?A)&B or ?(A&B) or even (?A)|B. >> >> I personally don't think this ambiguity exists: (?A)|B is strictly the >> same >> as A&B, thus ?A&B cannot also mean A&B, and I don't get how one could take >> ?A&B for (?A)|B. >> > > To me, the `|` (instead of `&`) in the original "is it `(?A)|B` or > `?(A&B)`" was just a blatant typo. By the way, I think you made a couple of > typos yourself: [...] > You're right, sorry about that... As for supporting the `?A&B` syntax, several issues were raised: > - Ambiguity: We would want the `?` "unary operator" to have a "lower > precedence" than the `&` "binary operator" (like `null|A&B` or explicit > `null|(A&B)`), which would be the opposite of familiar `!A&B` for "true" > operators. There was also some opposition to "implicit precedence" / > preference for requiring explicit grouping. > As I wrote in my original message, I don't think this one holds: ?X&Y cannot mean (?X)&Y because this is strictly and trivially equivalent to X&Y. > - Inconsistency: The similar `?A|B` syntax was explicitly rejected when > introducing union types. Moreover the future scope anticipates full > composite types, allowing arbitrary `A&B|C` (or `(A&B)|C > The features we ship should be fully fledged. I don't know the future, and nobody does. Anyway, this doesn't prevent accepting ?X&Y since the syntax in your example doesn't conflict. > And the main obstacle to supporting general composite types (whatever the > syntax): Unknown variance/LSP correctness (apparently complex to > specify/implement): George expressed themself in > https://wiki.php.net/rfc/pure-intersection-types#composite_types_ie_mixing_union_and_intersection_types > : > Here is the PR, no need for suppositions. Please send me test cases about those unknown and let's discover them if they need to. https://github.com/php/php-src/pull/7259 > That said, to me it also feels like the `null` type/value is "special", > and that PHP has a history of "nullability" being more than "just a union > where one of the types is `null`", making "nullable intersection types" > desirable (even without waiting for [hypothetical] full composite types). > Absolutely, null is special in the codebase, that's why the patch in the linkedPR is trivial. Thanks for your answer btw ! Nicolas --00000000000023326905c77aba64--