Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:123742 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 87FAF1A009C for ; Fri, 21 Jun 2024 18:29:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1718994656; bh=pxAsKOkiZ7PnBAJPIpkOMIvXjLyQ4lxg5TePbY8JZjg=; h=In-Reply-To:References:Date:From:To:Subject:From; b=Kl53K9sRtHssYxUNimSuDXUTuvvv3TfCIQDGAPofZdONz5WZIYVf0pkVkE72WdH/6 2jALUbFleuL4Gb442mJkgKRVrSgozx7kFMHTCYSS+J5GFdEHtCOkeTT/QurURPXQWT EX2wXFTE5eFCYOmOoMUe+tFxYcQv9LJdPU1B0tcA0qdALbhbYwJxxnY3kdVrB6NyW6 cFq+n0eLPLUyoSaUs3OOB/Vv5raBfvD8CFn1bwuetW/Y3TbVWDRTfN2rhZlYXC2JOF WZ+Y+ESKB518DyDW4NkJkTOLG4qx5SxeW7YEGehfAlwS9cNpk0kksNz1GAlnRWbcKK n3ukfblUsG0GA== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 245E7180888 for ; Fri, 21 Jun 2024 18:30:56 +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.1 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_MISSING,RCVD_IN_DNSWL_LOW, SPF_HELO_PASS,SPF_NONE,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 wfhigh7-smtp.messagingengine.com (wfhigh7-smtp.messagingengine.com [64.147.123.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 ; Fri, 21 Jun 2024 18:30:55 +0000 (UTC) Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailfhigh.west.internal (Postfix) with ESMTP id 20DBE18000C2 for ; Fri, 21 Jun 2024 14:29:40 -0400 (EDT) Received: from imap50 ([10.202.2.100]) by compute1.internal (MEProxy); Fri, 21 Jun 2024 14:29:40 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= garfieldtech.com; 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=fm2; t=1718994579; x= 1719080979; bh=5yNG6EUXZc/jFa645aO8xCdOxVog4yItI83Iaz4abOE=; b=J gCdJ1SvXmb4pQJJjgLTrLGR3gqMjRkWs1tfNqy0CmI0Hj9XbVPyUjc3TrE7MZ0Gs F9r3ugJhE84LV7BjjMzDAS3bG291jH9Fzz0YDvhzWBmCe0dOa6xhR8g3nsjYO3Zn RPGEaX+xFtbjuLMqFIYI2aQfV4uBSc7gpXgvbOiEvg/DpmYCeRhgQUv9RWYeB9VS XdqxBNzIL8fkIBKs6RVoDmBXCoJF8OqupFK4H5CHs27A257sYVdnukx+bbVz2Iph 3FkNqBq82dxERSdXQy9dYvT3lX5fL/qAgQ2QSwnsmWVaEzYM/fChyBwS9ObwvLx7 g1JiXOO0XntbQJxSiWKDA== 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=1718994579; x=1719080979; bh=5yNG6EUXZc/jFa645aO8xCdOxVog 4yItI83Iaz4abOE=; b=V2J/TVj0ztajPzkbjIZuisdc8zQHz5Cx9qSkHgfRyS9W HPn6LYiIR3fkU+fdYGVs6YlRDEkW8SC2Fx61jD+5rhtXiKMVGw3/RsuagsNeDH7h Ip4lphut6SkNDvGB8tUwbRIMuNZx4PkMg91h2NNxBd+RAaaP5c98+8OVRZEInQEj dwJ9PK1wITkUb7QC3CspUYUCEPIcGedP00V4ZHBzqAV1N8WF59vY3TEyta9PWsr0 uilkigTF/B04DeOmLNvCCO/SvuKOsjznjJoqcpnMJ0YZwMNv9BjD1kbT25NQpJU6 QnkMZK3Df1Cgzb9VwXSWlCaK9bwR2Xb4WbEFfqsqPw== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrfeefgedguddviecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enucfjughrpefofgggkfgjfhffhffvufgtsehttdertderredtnecuhfhrohhmpedfnfgr rhhrhicuifgrrhhfihgvlhgufdcuoehlrghrrhihsehgrghrfhhivghlughtvggthhdrtg homheqnecuggftrfgrthhtvghrnhepgeelgfekudeivddvteffueejffdthfejieevhefg ffekudevkedtvdelvddvffefnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpe hmrghilhhfrhhomheplhgrrhhrhiesghgrrhhfihgvlhguthgvtghhrdgtohhm X-ME-Proxy: Feedback-ID: i8414410d:Fastmail Received: by mailuser.nyi.internal (Postfix, from userid 501) id 694941700093; Fri, 21 Jun 2024 14:29:39 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.11.0-alpha0-522-ga39cca1d5-fm-20240610.002-ga39cca1d Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net MIME-Version: 1.0 Message-ID: In-Reply-To: <978b7177-8a22-41c0-94ce-d5539a2468c5@app.fastmail.com> References: <2a6b92eb-d5e9-4a1a-9548-a068ac42ebd2@app.fastmail.com> <978b7177-8a22-41c0-94ce-d5539a2468c5@app.fastmail.com> Date: Fri, 21 Jun 2024 18:29:19 +0000 To: "php internals" Subject: Re: [PHP-DEV] [Early Feedback] Pattern matching Content-Type: text/plain From: larry@garfieldtech.com ("Larry Garfield") On Fri, Jun 21, 2024, at 12:38 PM, Rowan Tommins [IMSoP] wrote: > On Thu, 20 Jun 2024, at 18:38, Larry Garfield wrote: >> Hello, peoples. >> >> Ilija and I have been working on and off on an RFC for pattern matching >> since the early work on Enumerations. A number of people have noticed >> and said they're looking forward to it. > > Hi Larry, > > I haven't time to read through the full RFC at the moment, but a couple > of thoughts: > > As Andreas says, we should be careful not to pre-empt things that might > be added to the type system in general, and end up with incompatible > syntax or semantics. That particularly applies to the generic-like > array syntax, which is quite likely to end up in the language in > some form. As noted in another thread, I don't believe that would cause any engine-level conflicts. Whether it would cause human-level conflicts is another, and valid, question. > The "weak-mode flag" seems useful at first glance, but unfortunately > PHP has multiple sets of coercion rules, and some are ... not great. > It's also not immediately obvious which contexts should actually > perform coercion, and which should just assert that it's *possible* > (e.g. match($foo) is { ~int => (int)$foo } feels redundant). So I think > that would need its own RFC to avoid being stuck with something > sub-optimal. We *still* have different implicit coercion rules? I assumed it would be implemented to match weak-mode parameters, not casting. Though I agree, there are devils in the details on this one. > Similarly, the "as" keyword has potential, but I'm not sure about the > naming, and whether it should be more than one feature. Asserting a > type, casting between types, and de-structuring a type are all > different use cases: > > $input = '123'; $id = $input as int; // looks like a cast, but actually > an assertion which will fail? > $handler as SpecialHandler; // looks like an unused expression, but > actually an assertion? > $position as [$x, $y]; // looks like its writing to $position, but > actually the same as [$x, $y] = $position? > > It's worth noting that in languages which statically track the type of > a variable, "$foo = $bar as SomeInterface" is actually a type of object > cast; but in PHP, it's the value that tracks the type, and interfaces > are "duck-typed", so it would be equivalent to "assert($bar is > SomeInterface); $foo = $bar;" which isn't quite the same thing. Valid points. The line between validation and casting is a bit squishy, as some casts can be forced (eg, string to int gives 0 sometimes), and others just cannot (casting to an object). So would $a as array<~int> be casting, validating, or both? Patterns make sense for validating, so it's natural to look to them for validate-and-cast. Though I recognize it could then complicate the cast-only case, if it exists. --Larry Garfield