Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:123760 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 CEE111A009C for ; Sat, 22 Jun 2024 21:57:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1719093534; bh=ZTxVGqE1KfgbogXqpAoX7Tj1O2U7HTRfPexubhMBjI4=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=RgLIFAhARzi+xXEbxNLIJ3edhz4BcWSKp+0hJzkkHIEwG2Tuxdbq6FLf5mz0Uf8si 9Pqk0eLEblpzoi/xVGAoVE/w55YyZtDumyWjjXtVhGqLXKHMQyYlZ1FufVJXEcEd7l CIYREnmNBXyiOfEB/UGGcSgkkRy5weetRYkg+jrnvTpBda/atTze0IheIyc/wG6HSU GqcNEi6WlivOcWEetzhuKsupy+lmhsQ0OOQnvvqtfYg0/Q1z2ZVyy8qxC1hkrx1u7f We2t8DHPv8gXJeuRiEZ5j99aT4ZAjJ3+CuJTXLAsC5nahHjNUXwyWjQCv4ToauHjXp M0rzOoLGqM90w== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 9FF761806EC for ; Sat, 22 Jun 2024 21:58:53 +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=0.6 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,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 mail-lf1-f47.google.com (mail-lf1-f47.google.com [209.85.167.47]) (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 ; Sat, 22 Jun 2024 21:58:53 +0000 (UTC) Received: by mail-lf1-f47.google.com with SMTP id 2adb3069b0e04-52c94cf4c9bso4027456e87.2 for ; Sat, 22 Jun 2024 14:57:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1719093456; x=1719698256; darn=lists.php.net; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=kjAXFNFF6UcxK8ufRqIroxm0VvSLfhhRk7f1nwogILI=; b=VXX6bRlqsj5CQ2Rxsb1Ra0aEfjOD7891MX0U+vDLkzCeh0D/oB0y2+LgTZPo5tNhP4 jnXv+WP+EsZtbId+dnTQSANaonPtsDQ0zEQsTHlKUdFQwyYZ3GiEPn2CdKpxwPcVBDDH U7hQy9xY8WdQx+SljD5xXzA4drXJckKm3WZrycNXgeAvC1kSgWUAm9IJPBxlcUh+oJgr CPtAgDB6dzttbZM/w8gKV26TQaOBQp0tn32NrJkI2PQcxpV/GIc/esE2L1jzsTJaOIun 9arBAY84G5LBqLQ4UwbM/5huiQrosCzTlVB5mr260rLnHiHX9p4dUHcFxl6jZblTdJoR T7Mg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719093456; x=1719698256; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=kjAXFNFF6UcxK8ufRqIroxm0VvSLfhhRk7f1nwogILI=; b=veLLmLpAr/OWBbKY74lVYUbJk8JTtqORJUplVpSnTzAxS9gVGK92k49p/74SDvGOTp iIoQPES8vvi9dzyECOExPhYow+vd3rt/FyoJFX96rAL157HDXa9rWCi4JQ98eesu9tyG ZVbdfYyN1gp81TUWNjmOd5MGUYdI5Ac17ekxeY4I4+hS+hCE0BERcYTYVmbNNVhzrEX8 fHW8jHCOySDQmd9qdo3oGoNjoXsBKRKpSMdaW8WuE1U3cJ46+Ne+cIzUWua99jiW7KaH 4heRPQ+z1PCJ0cL6MR44LPaqawvrC+ZIbn/MknGzA1f99aOM2D5Pdqrb7nwZqRKGRYl7 b6wQ== X-Gm-Message-State: AOJu0YwJSmZeX/PEtTKWzwPNryBvT98rEFkFsIb4i/LqBmJtysUBpwMB d9SPJctMsHGdfrW4Z0Ceg+tquh9w3KFV8RnH1PkoI9Wpm+yoDq4kb2DNRjd19gcWRPLEtXeMJaW Dpjjt5KKiNE2bRtk1fKg++g7p9MmDzyMz X-Google-Smtp-Source: AGHT+IHdO88DdPwqkIl1H2uzfXieD5+qAPqQ3vOlKEJ3OiGwyal5LEe6xKFcxMHaymIKhf/359k4Txo+YrIAMqzm9l0= X-Received: by 2002:a05:6512:2254:b0:52c:df55:e11a with SMTP id 2adb3069b0e04-52ce18325f3mr655419e87.9.1719093455925; Sat, 22 Jun 2024 14:57:35 -0700 (PDT) Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net MIME-Version: 1.0 References: <2a6b92eb-d5e9-4a1a-9548-a068ac42ebd2@app.fastmail.com> <978b7177-8a22-41c0-94ce-d5539a2468c5@app.fastmail.com> In-Reply-To: Date: Sat, 22 Jun 2024 23:57:24 +0200 Message-ID: Subject: Re: [PHP-DEV] [Early Feedback] Pattern matching To: "Rowan Tommins [IMSoP]" Cc: internals@lists.php.net Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable From: landers.robert@gmail.com (Robert Landers) On Sat, Jun 22, 2024 at 10:53=E2=80=AFPM Rowan Tommins [IMSoP] wrote: > > On 22/06/2024 19:34, Robert Landers wrote: > > I've brought this up before, but I mostly see "as" being useful for > static analysis. That's what I've mostly used it for C#, anyway. > Logically, you know the type, but due to one-thing-or-another you > can't "prove" the type is that type (such as foreach-arrays or dealing > with results from user-code callbacks in library code). I want to be > able to say "this is an int or else." > > > I absolutely see the use case for that; I just don't think "as" is a good= word for it, because that's not what it means in normal English. > > > Incidentally, according to the C# docs at https://learn.microsoft.com/en-= us/dotnet/csharp/language-reference/operators/type-testing-and-cast#as-oper= ator > > > The as operator explicitly converts the result of an expression to a gi= ven reference or nullable value type. If the conversion isn't possible, the= as operator returns null. Unlike a cast expression, the as operator never = throws an exception. > > So it more closely matches my intuition: a statement of just "foo as Bar;= " would be useless, because it's calculating a value and discarding it, wit= h no side effects. In general, you assign the result of the operation so that the output is useful. Here's how that might look in PHP with the C# rules: function foo(BarInterface $bar) { $baz =3D $bar as Baz; $baz?->thing(); $bar->otherThing(); } With "is" then it looks a little more wonky but isn't far from the current instanceof method: function foo(BarInterface $bar) { if ( $bar is Baz ) $bar->thing(); $bar->otherThing(); } With fibers/async, "as" is actually more important than "is" (at least as far as crashing goes): class Foo { public BarInterface $bar; public function doStuff() { $baz =3D $this->bar as Baz; // some stuff with $baz callComplexThing(); // suspends current fiber, // $this->bar is no longer the same object // or concrete type when we return $baz->something(); } } If we were to do an "is" check on the first line, by the time the fiber is resumed, we've got a completely different type on our hands and it would crash. Maybe that is desirable, maybe not, but we know that we have a reference of the type we want and it won't be changed under us by using "as." > As you say, the conversion might not be of the value, but of the statical= ly analysed type, but in C#, that's all part of the language. In PHP "$foo = =3D $bar as SomeInterface;" would have no visible effect except in third-pa= rty tooling, where it can already be written "/** @var SomeInterface $foo *= / $foo =3D $bar;" Hopefully my examples show how it can be useful (at least when it returns null if it is the wrong type). When it gives a TypeError or something, it becomes far less useful -- at least for the sake of conciseness. However, it becomes far more useful to dealing with scalar casts: function foo(int $type) {} foo(123.456 as int); // crashes foo(null as int); // crashes But even if we did return null, those would crash unless foo() took int|null, which may or may not be what you want ... With it always being an error if it doesn't match, it's really not that useful, as you point out. > > > -- > Rowan Tommins > [IMSoP]