Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:123754 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 2FE431A009C for ; Sat, 22 Jun 2024 18:48:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1719082162; bh=V4A0g2x5y1Jk39UZ6oQnwwZRAtb43gOoRmQvaGBx+Rk=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=fgCN0Ll9KTZMsPHVxQjtHO1pcA9FRXwGYXBVe+/HblYDmZ+0K4biHH8U0xDmvz0ZK cnhsmZHjApztZilDr8Y6/91oXkTpSxNOl5fOcqL7C+1nv6/NveySMpAUN1fB0m56Xn 3L5P78NXCYKtwSbw4ZuP3sbJ9vmJLPaa0R2EAN+VuDYhZ6oTnDE5L5jbb+DphZ8MZn aak280Ecerh3Jsdjjz+MbHAsac1CmBlcwDwiOgAfFeoJ6Ar2W1Ui8fPw7ZeiRh1BG9 aPNI0SrytieQzNusZBTSf3EreLup/9xD/f38INM3bJhPM9KCHUimMCL3AzKz4LkIzb a6TtSISVtRj2g== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 82F1F18063D for ; Sat, 22 Jun 2024 18:49:21 +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-f46.google.com (mail-lf1-f46.google.com [209.85.167.46]) (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 18:49:21 +0000 (UTC) Received: by mail-lf1-f46.google.com with SMTP id 2adb3069b0e04-52c94cf4c9bso3921894e87.2 for ; Sat, 22 Jun 2024 11:48:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1719082084; x=1719686884; 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=psMPU8uF4hwFicUsW/9rfDdroWoueUk2Z7sxmjowABs=; b=narFA3PwtLs9Mi4bgdwdaux+yi+kHuCRTCYoUH1uNqhVKRI5iHZhyOKoi18OMS8SNn ukC8zXSMQrX0WfxMQE4UHW/LxPVEOpn5dRFrI1gW5lqRzLFpJdEmDw4ZqfyBPuNRwtXo yMpuZkw9nsO9mMJuIrhDTKuVpi7zS/OyUJXQz82sJ7M1ywH07HOSTFSWnMCDQ/+nYVip 8ib7+o0I90QGF/6THNrBM4hYLUz8mZrw8NXHe/cPiCXrX8n6lcv3LpOOZBNiRdL13PI7 11uNRA3vXzVIafmlwL7LgqH7sR9J0cWMr0F5wUxM09Wzyl63vtwNw7+3W3Q1qFUMRnUU VOlQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719082084; x=1719686884; 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=psMPU8uF4hwFicUsW/9rfDdroWoueUk2Z7sxmjowABs=; b=wVFpuOREXvjRZKJQb/9arhwTjGPKo8Ckn0eyTjv3oRqwCaEwY7rPTS+BLYahU2fYXJ IyAJeMvsAVhZFZi0aivl6DHFCC16De9/7+0aFELgY/VWd7T1CDnktPKuqxhMLCs00tbP /vcd1chJbwXWVCyREYVouzfHtD0x0su2+2MpZ2fXv2GyLmDC3ePpLXTy8VIPZGl4lUK+ BENXghXLRh20a/9vPkiRcHtTTZFExY6Wn2SoeaOwKo/EZq6e+3yi7ZMSzTZcL42xjVp1 xWaBI+thhZtHFHsZXfbHGO27opkq4N4FwagKdbnaI5JwPVJxjCf8J/gi55CUSXiPWohH n6fQ== X-Gm-Message-State: AOJu0Yz8RmbwHc+0UUvr2Bwe/vrkkq392DYWhCa6iek7h9iUBbexmw6b My/CGodD2F39WB1lk8NyFga4NOzojJWHarNxED4YZb07m/FePrGqR8/vcYGeqGjdRKeqhPbiPQt TcQ1CuswnGMVCNDC3NDY+TGVVZGsGjhriGZg= X-Google-Smtp-Source: AGHT+IEuQaPHyabDAkzAKJDrU8BJNpE33tpr/9x80MI4cJ5o7w6HmtqAQ+7qAzM01AfZamLY2FGzbihpQ+5PIbamSAc= X-Received: by 2002:a05:6512:515:b0:52c:deb9:b974 with SMTP id 2adb3069b0e04-52ce1863223mr276149e87.69.1719082084027; Sat, 22 Jun 2024 11:48:04 -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 20:47:52 +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 8:34=E2=80=AFPM Robert Landers wrote: > > On Sat, Jun 22, 2024 at 8:04=E2=80=AFPM Rowan Tommins [IMSoP] > wrote: > > > > On 21/06/2024 19:29, Larry Garfield wrote: > > > > 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? > > > > > > I think my concern is that both "x is T" and "x as T" read naturally as= *expressions*, where their main purpose is to evaluate to a result, and si= de-effects are exceptional. > > > > From that point of view, we can give intuitive meaning to the following= : > > > > - $foo is int =3D> boolean; is $foo of type int? > > - $foo is ~int =3D> boolean; can $foo be "safely" cast to int? > > - $foo as ~int =3D> int; cast $foo to int (unless unsafe) > > > > But then what does this mean? > > > > - $foo as int =3D> int; cast $foo to int if it's already an int !? > > 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." > > [snip] > > Now ... that being said, I'm def not a fan of this "~" thing. It makes > no sense to me. Semantically, what is the difference between "123", > 123, and 123.0 other than how the bits are arranged in memory? If it > can become an int -- regardless of how it is laid out in memory -- I > expect to end up with an int as long as it is an integer. I would find > it super annoying to have to fix that bug report with a single > character, just because someone returned 123.0 instead of 123 because > they rounded the number and I got the result from a callback. In other words, I want what I currently get from mode 0, but instead of a warning, an error; but also not the sledgehammer of direct casting in mode 1. In mode 0, this is currently a warning (fn(int $x) =3D> print($x))(123.1); and this is fine: (fn(int $x) =3D> print($x))(123.0); But if you are used to working in mode 1, both are a fatal error: // strict_types=3D1 (fn(int $x) =3D> print($x))((int)123.1); Which is probably much worse -- depending on your business -- because you don't get any notice that things are going wrong; it's completely silent. What I would find (and I think everyone would find, IMHO) is that you actually want to cast to int if it can be cast exactly to that type, otherwise fail. (fn(int $x) =3D> print($x))(123.1 as int); // crash (fn(int $x) =3D> print($x))(123.0 as int); // success! > > Further ~ means bitwise-not, so when you start mixing in literals ... > what does it mean? > > $y =3D $x as ~1|~float; > > What is the value of $y? Is it the literal negative two, casted to the > literal one (int), or casted a float?