Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:123927 X-Original-To: internals@lists.php.net Delivered-To: internals@lists.php.net Received: from php-smtp4.php.net (php-smtp4.php.net [45.112.84.5]) by qa.php.net (Postfix) with ESMTPS id 59B4C1A009C for ; Thu, 27 Jun 2024 06:57:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1719471544; bh=qdZSnFzCq/KYNd/LLrGuW5CdhRTYAt58cS1ENsDyho8=; h=In-Reply-To:References:Date:From:To:Subject:From; b=HxX1aEgTwu4l8eZIceoWTOK6HtvxUjf3z9EDDwQRf0PouMF+X5eixpoqVVCgCOKzS A70GZ3Q+3jy0hfbppdMW5PG5ozGgP4JdKRtKH8cmqcz95ujnLD+RS3YAqPm7h1p9+S lVo77Y1Y3mX4JuxuHKCOxOB18Sl552vgwZwPGUvpram2qY4SecyW0ABzG3JHqk0Kog 1qy1qmedskCevQMM4QJEQbYjJesVlpEkNzslvrP3fdDKrx5n2Cpcqj6wFUvKxvt2HX 6yCl/EuZtNi3YRCCZpEhuSMLsiONImrZQ4rDOJ8HWqs6YbQfLHSzsFvn1ZfCOnh3mO tmLsPx3Ik9v/A== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 8952518005F for ; Thu, 27 Jun 2024 06:59:02 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.0 (2022-12-13) on php-smtp4.php.net X-Spam-Level: * X-Spam-Status: No, score=1.4 required=5.0 tests=BAYES_50,BODY_8BITS, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_MISSING, HTML_MESSAGE,RCVD_IN_DNSWL_LOW,RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL, SPF_HELO_PASS,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: Error (Cannot connect to unix socket '/var/run/clamav/clamd.ctl': connect: Connection refused) X-Envelope-From: Received: from fhigh7-smtp.messagingengine.com (fhigh7-smtp.messagingengine.com [103.168.172.158]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Thu, 27 Jun 2024 06:59:01 +0000 (UTC) Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailfhigh.nyi.internal (Postfix) with ESMTP id 87006114019E for ; Thu, 27 Jun 2024 02:57:43 -0400 (EDT) Received: from imap49 ([10.202.2.99]) by compute1.internal (MEProxy); Thu, 27 Jun 2024 02:57:43 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bottled.codes; h=cc:content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm3; t=1719471463; x=1719557863; bh=7n2KBbD5x1 afdTTu2+VLrWfkCT8MlguVpyr7hnpaBgU=; b=p/gojIlkQbm1Uo0WF8ZiC2Yadm 0nz3bPdkmUN1ffXxaCv1LJ12y+J4VvgBEmXuYNKtQrSrOj/PSpwzzOmEwq0ZODVH rQSM/yP30Im3j80x7TKcsXZiCt9geYJ1CvLK/17AtXRN85D6VK34bv9IGwJXLJjr sZNkxOH2dcVC+DsiZ/rclZZXBwnMLq/9QjZP3SZe/cSqlgEYrhJe3MQUktcN7QY1 57z5uUijL/4Ati0aI9QVmrljQbyzxthOr6PZMNSQHR/OgRrq2/6XRYn/RItwL7uD HzgFTzPlaXKJUJmOHTXu8xak3Xt2WV6bEv00nqkbl2WNUVkcCBAfMpJ1z4IQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm2; t=1719471463; x=1719557863; bh=7n2KBbD5x1afdTTu2+VLrWfkCT8M lguVpyr7hnpaBgU=; b=gwh9ELryMUvOEHpYgwsSq52csuegYknJbCqLUcbfj6Xw UUmu0vJcN+UhkwmN/lrQqrbrb+6msxbrgSRdaealhSrPen2umTU9IGmFrj/Dhj8h vPim5DkSeQdHctiVZt41VEQ7ZhN+3gp4jlwDUOxfIGAif4LbyOSXfMDhXEyv+X3G NveHkYwIwQRfrsPTyJpGlYM++3MPEK+HF4U8jDE7PxdTHDXBZEq0rQGGFFiH4vgi h/liriF3SHgd8h873Be/e2GHMUtySUy8cTCbwoJ4KqbUgyjuMUmgcOHeE/3A3ICC v084OmFyhj8/xIyVwpZXQ75e2dDzWlfC9TLaeTo1DQ== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrtdefgdduudegucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucenucfjughrpefofgggkfgjfhffhffvufgtsegrtd erreerreejnecuhfhrohhmpedftfhosgcunfgrnhguvghrshdfuceorhhosgessghothht lhgvugdrtghouggvsheqnecuggftrfgrthhtvghrnhepfeefudfhudduieekkedugffhud fgleejgfekgefhvdeikeelvddvjeehteegteegnecuvehluhhsthgvrhfuihiivgeptden ucfrrghrrghmpehmrghilhhfrhhomheprhhosgessghothhtlhgvugdrtghouggvsh X-ME-Proxy: Feedback-ID: ifab94697:Fastmail Received: by mailuser.nyi.internal (Postfix, from userid 501) id 1DA4715A0092; Thu, 27 Jun 2024 02:57:43 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.11.0-alpha0-538-g1508afaa2-fm-20240616.001-g1508afaa Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net MIME-Version: 1.0 Message-ID: <0802b731-cbe6-4353-b554-b57b9f74a2c5@app.fastmail.com> In-Reply-To: References: <2a6b92eb-d5e9-4a1a-9548-a068ac42ebd2@app.fastmail.com> <1E295280-619B-4490-B53C-0899B64F9215@chaz.works> <97a93ae2-5202-47eb-bf51-ec1e976ea765@app.fastmail.com> <78D0FFF7-3867-4D71-B0C0-FA23E5121C39@rwec.co.uk> <10390963-0c2c-441a-bcce-20a0433948c5@app.fastmail.com> <8498ecf7-f431-4d68-aaf1-dc4cfb00241b@app.fastmail.com> Date: Thu, 27 Jun 2024 08:57:22 +0200 To: internals@lists.php.net Subject: Re: [PHP-DEV] [Early Feedback] Pattern matching Content-Type: multipart/alternative; boundary=8bb83fd6aaeb44a88e5cd970954c4a46 From: rob@bottled.codes ("Rob Landers") --8bb83fd6aaeb44a88e5cd970954c4a46 Content-Type: text/plain;charset=utf-8 Content-Transfer-Encoding: quoted-printable On Wed, Jun 26, 2024, at 23:42, Ilija Tovilo wrote: >=20 > function test($value) { > if ($value is ['foo' =3D> ?string]) { > $foo =3D $value['foo']; > } > } > test([]); (Scroll to the end if you don=E2=80=99t care about my rebuttal) I mean, there=E2=80=99s nothing wrong with this code, $foo will be null = at the end. >=20 > With your approach, the example above would emit a warning, even > though the context within test() doesn't look like it should.=20 That happens every time I get one of these warnings. It=E2=80=99s nice t= o get the heads up that I didn=E2=80=99t think through something and nee= d to either A) figure out how it got there, or B) provide a default =E2=80= =94 even if it is null =E2=80=94 to make the warning go away.=20 > If ?string means that the index might or might not exist, all code that > accesses them must check for existence, even when not needing to > handle null itself. That doesn't seem desirable to me. This is true already. since this warning and null-coalescence, I can pro= bably count on one hand the number of times I=E2=80=99ve *not* written s= omething like ?? null beside an array access (though I prefer throwing a= n exception if appropriate).=20 >=20 > I also think the issue goes further. If anything|null means that the > offset might not exist, does that include mixed? That makes ['foo' =3D> > mixed] essentially useless. In thinking about it some more, for lists, it would be nice if there wer= e two modes: 1. regular mode: $a is [1,2,3] where it must match exactly.=20 2. set mode: $a is set([1,2,3]) where order doesn=E2=80=99t matter, onl= y that $a contains at least one of every given element.=20 =E2=80=94=E2=80=94=E2=80=94=E2=80=94=E2=80=94=E2=80=94 In other news, I finally found a realistic example where there is a diff= erence between non-existence and null. The scenario is this: Imagine you accept a callback and you prepare an array to pass as parame= ters. In this case, you call the callback like so: $callback(=E2=80=A6$args); Today, you either have to yolo it and hope everything is perfect or meti= culously check the args to make sure the types match your documented cal= lback signature. If a key in $args doesn=E2=80=99t exist, it might be an error (or defaul= t value provided by the callback implementation) but if it is null, it m= ight also be disastrously incorrect because the default value won=E2=80=99= t be used.=20 By using pattern matching, we can check the structure of the array for n= on-existence or if exists, the documented type. This is where matching a= list exactly is important (if using positional args). So, now that there is a use case for it, we can work out expected behavi= ors of lists and associative arrays and nullability for both. Anyway, does anyone have a suggestion for what kind of hat I should have= for dinner? =E2=80=94 Rob --8bb83fd6aaeb44a88e5cd970954c4a46 Content-Type: text/html;charset=utf-8 Content-Transfer-Encoding: quoted-printable
On Wed, Jun 26,= 2024, at 23:42, Ilija Tovilo wrote:

function test($value) {
    if ($value is ['foo' =3D> ?string]) {
        $foo =3D $value['foo= '];
    }
}
tes= t([]);

(Scroll to the end if y= ou don=E2=80=99t care about my rebuttal)

I mean= , there=E2=80=99s nothing wrong with this code, $foo will be null at the= end.


With your approach, the example above would emit = a warning, even
though the context within test() doesn't l= ook like it should.

That happ= ens every time I get one of these warnings. It=E2=80=99s nice to get the= heads up that I didn=E2=80=99t think through something and need to eith= er A) figure out how it got there, or B) provide a default =E2=80=94 eve= n if it is null =E2=80=94 to make the warning go away. 
<= br>
If ?string = means that the index might or might not exist, all code that
accesses them must check for existence, even when not needing to
<= /div>
handle null itself. That doesn't seem desirable to me.

This is true already. since this warn= ing and null-coalescence, I can probably count on one hand the number of= times I=E2=80=99ve not written something like ?? null besid= e an array access (though I prefer throwing an exception if appropriate)= . 


I also think the issue goes further. If anything|= null means that the
offset might not exist, does that incl= ude mixed? That makes ['foo' =3D>
mixed] essentially us= eless.

In thinking about it so= me more, for lists, it would be nice if there were two modes:
<= ol>
  • regular mode: $a is [1,2,3] where it must match exactly. 
  • set mode: $a is set([1,2,3]) where order doesn=E2=80=99t matte= r, only that $a contains at least one of every given element. 
    <= /li>
    =E2=80=94=E2=80=94=E2=80=94=E2=80=94=E2=80=94=E2=80=94
    =

    In other news, I finally found a realistic exa= mple where there is a difference between non-existence and null.

    The scenario is this:

    Imagine you accept a callback and you prepare an array to pass as param= eters. In this case, you call the callback like so:

    $callback(=E2=80=A6$args);

    Today, = you either have to yolo it and hope everything is perfect or meticulousl= y check the args to make sure the types match your documented callback s= ignature.

    If a key in $args doesn=E2=80=99t= exist, it might be an error (or default value provided by the callback = implementation) but if it is null, it might also be disastrously incorre= ct because the default value won=E2=80=99t be used. 
    =
    By using pattern matching, we can check the structure of = the array for non-existence or if exists, the documented type. This is w= here matching a list exactly is important (if using positional args).

    So, now that there is a use case for it, we c= an work out expected behaviors of lists and associative arrays and nulla= bility for both.

    Anyway, does anyone have a= suggestion for what kind of hat I should have for dinner?

    =E2=80=94 Rob
    --8bb83fd6aaeb44a88e5cd970954c4a46--