Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:123761 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 2A22F1A009C for ; Sat, 22 Jun 2024 22:05:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1719094008; bh=4U1ZteODlIsECvS/T9LL+gWOp9df7AsgBA0dgrPa9Iw=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=GYi8oT5zIfQjYbCWtuqucFY04R1zHSgeqBJjfuszY5sqh/BvGlTfaHZA3TTlmaEJb hN65b5nIBj9MRK69clDbmQq2C6TI/sm8VAb7WxxvYCk/y0KHqwBUNDWqf2mbpQmjrp E7Sq1d+TSYT9kVd+lyNVvKZ4pb4Iq8OQJqgVgBRP+kU5tz3Hn4E/0vf7j72Z2CS+Nl Fp6ov4/JNyQ9xx/1g/G3MLcV9IxJyUfODBeDzKUGOrNoKjUFyq7RzAY0SlvwEXyxjZ +5Xxd4DX9TpB47mq+fWSoAq0HM+uWH0e0epKTvGMP8hTH92jbwIOhsG1/ug00KbehM lEqhjGSVQcZXg== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 9EF4C18007D for ; Sat, 22 Jun 2024 22:06:47 +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-f45.google.com (mail-lf1-f45.google.com [209.85.167.45]) (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 22:06:47 +0000 (UTC) Received: by mail-lf1-f45.google.com with SMTP id 2adb3069b0e04-52cd8897c73so1805896e87.2 for ; Sat, 22 Jun 2024 15:05:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1719093930; x=1719698730; 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=WHXxYrjGKSB2GgJVcieyYZCPWEB4j2Ntj/TszWxD4Qk=; b=ObL4Ld8ky9KZFWc+2+SHMmdszGkCwUgf60BB32qez/xCwdFx2gVJxQSb4l5MYJj7T0 ZvdC9+ChEAOFq6o31HrbN8vWBQjOzLBShXdAvLOgkO04PdyPSltaYB54Q+RTYUZmVXsE hMICxYVBDzgf1DOnxrqC6cHZD1he1LIYfy7oxz+mGR8HoDPifVnOpc9DJnY800xwVKEt 4X3c949eUE/vK4zAAgxyxNBClRtLBR6cYutFwtzB4r2BzTj0aGOUB5kzXJCx9esRLoWj OtMFObZ0TxpE368ITHYyEP29OmnB9YZa5I1DJJ3UHZGS9RdBsdQX5xBgQoqTfesTgVnq 0oxg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719093930; x=1719698730; 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=WHXxYrjGKSB2GgJVcieyYZCPWEB4j2Ntj/TszWxD4Qk=; b=KwIYMJmpz54pRPcFK0Ytmh3OAyHCLpCfUmRRalmkztZO5tJjzc5DawU/mNMEw9mgmy npsMbxumCRieesPklxT5wR45f9l6OI09gQLGpfLF9DzLYjivvaa4A/XKPdGjYH4iSeHX VlfYHpBpsTiHL2HCR+MUYgHNA7FblhdQokVwiwSN1/BXmzifb1lt92F9AOcyHHvyisYT Bhzq5w5juRd2cMtA9Rj1yMduhWz10+bF7GsMRMuyobBlzm6h9xTnNnLY39wlijTpI9CX nujEKCqFomprlqODqK3vAfrZVJ2q1UeM5z4bGiN8gFfCUQ/hq55xOB8Fpaljozo9wcUX zjNQ== X-Gm-Message-State: AOJu0YxGpErlPp4xxIWQYFPpL4IQJJhYv34kZfFXAvVAHNx4aabExO/P 8VBgvFANuyc2BXl76yQJbkNd9+n68ZAZKJpCBXN1nAcg99xBFlxBCvuYv2HJRtWMK7CxIIHgP5f +ZFV4EX5zk9qHo/NaP6+UNs9C/TE= X-Google-Smtp-Source: AGHT+IHAgs07L9AzQYR/YJjC9iXr1IJzT8bg8E5jqnNvQvEDbTBswVeJ7Su4IMX1WZIffGznKh0B8ua+69uE4UwfiZE= X-Received: by 2002:a05:6512:3e24:b0:52c:987f:b355 with SMTP id 2adb3069b0e04-52ce185f66emr470190e87.42.1719093930154; Sat, 22 Jun 2024 15:05:30 -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: Sun, 23 Jun 2024 00:05:18 +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 11:57=E2=80=AFPM Robert Landers wrote: > > 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 go= od word for it, because that's not what it means in normal English. > > > > > > Incidentally, according to the C# docs at https://learn.microsoft.com/e= n-us/dotnet/csharp/language-reference/operators/type-testing-and-cast#as-op= erator > > > > > The as operator explicitly converts the result of an expression to a = given reference or nullable value type. If the conversion isn't possible, t= he as operator returns null. Unlike a cast expression, the as operator neve= r throws an exception. > > > > So it more closely matches my intuition: a statement of just "foo as Ba= r;" would be useless, because it's calculating a value and discarding it, w= ith 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 static= ally analysed type, but in C#, that's all part of the language. In PHP "$fo= o =3D $bar as SomeInterface;" would have no visible effect except in third-= party 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. Side-note: this is why my original proposal had two modes (based on the existence of null in the typecheck): foo(123.456 as int); would crash due to being unable to cleanly cast to int. foo(123.456 as int|null); would crash from passing null (since literally everything can be casted to null and it can't be casted to an int). > > > > > > > -- > > Rowan Tommins > > [IMSoP]