Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:122696 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 EE00C1AD8F6 for ; Tue, 19 Mar 2024 22:20:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1710886835; bh=fRFJgvYR8f1cENbMRYdf1G1z2Ejv4hPmsQM/Eqwz3G4=; h=References:In-Reply-To:From:Date:Subject:To:From; b=Quwyi+XSvTcVAqyeFIesUa7Gwivqt9Q08frWj86amZG4ktQozC4HuFEzTe8Cb1vFQ E7Fs6lHH2a6buPJxwQZ1TfSYwif+rR2pzxMucQmKAC83ssGeXKNBI8/VUhoyAXwTCz ZVjWf3FpBls8ZL2n3DX8ZhV/QBLhOn46blDIj5b15k8FN5RMv0MLIqba4PKUYda083 OcBbYE9VLiIe5ifmENc+mCXfHPflhP5OhUmax4OpXC5ssjo8r7/e7yFdOXResTzJ/e PZ+qdxib/TyN3O7V5NimbygHzuhld+tcC14bVZcyH86ySA53qhGAN01JngKe5L6pqD O93vyMcrkt60w== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 3205418003E for ; Tue, 19 Mar 2024 22:20:34 +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: No X-Envelope-From: Received: from mail-vk1-f181.google.com (mail-vk1-f181.google.com [209.85.221.181]) (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 ; Tue, 19 Mar 2024 22:20:33 +0000 (UTC) Received: by mail-vk1-f181.google.com with SMTP id 71dfb90a1353d-4d40ca07bf6so1365208e0c.3 for ; Tue, 19 Mar 2024 15:20:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1710886812; x=1711491612; darn=lists.php.net; h=content-transfer-encoding:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=fRFJgvYR8f1cENbMRYdf1G1z2Ejv4hPmsQM/Eqwz3G4=; b=Yt1pm2bZ2bpM4WEFbiTtDfM9YMUvDQklN0rqo7TMZEsEdUsOwjFRWEYSr5m0He0Wwa vE0YTBeEZp4N3wv+otwcJKS+fqCpyk3KGK7mYHPSPfpKnYx1xJF/tTl9Tk9JOl8YRtQX 1Fiif7E/MolnyyaI7S3dHGma5Or7KsdeQnld5oHmV9wnaArMjryK4nk05SLWN/zygpIq OvVeaAg8ndd67AmnY36ckxD8jhpn50TB1Kp/Pn72+8LGPQ98JoUEJp9jS9lt4xwDZtBx itl+6+IrPnNJwnMS08q2k3sbjZt80nCOPSVVZe4nj1kJQuKzjRUDT5WuMXl7TAlFIH4K Mo0w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710886812; x=1711491612; h=content-transfer-encoding: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=fRFJgvYR8f1cENbMRYdf1G1z2Ejv4hPmsQM/Eqwz3G4=; b=wBf4kabw459ETJns+P3dUXaLVHhaGX1D+6DaEUgrKIyTzk/KzQ1lweePlIeftDRoe+ plg7CFMq9Uz3hRvPjOTUxwhLZkuCW7X+sgaXCcdAnPqraLhaTANiX1wG8RTjgDQTJovj ffmoluIErOmMuQ0zTVkxLDLI9TKV1C/x8wY6QQudSwQzw1guZklDLeqPGqpFVLEhF7bO RCPKaUZhftnd55hcVBOyAet62M6kM4fyLi/a070kvI/Ce6S84v4drFZmUy1dou63pRCS ar+ZlA2V23D8MqoEJXECiI+QJrOVB66QirCjkcwwpevhfCvDDCXrQR/Fsh6UsR+8unh0 YviQ== X-Gm-Message-State: AOJu0Yw3mW4GLyISciakh8yrdWvYwUjksF6dia6p7cID7IPjDT8nUwYT npMMEWjeUDGGh4OWC0E1Raw5W3OMLagk7IOxjyR494v9fv/2RLQnAN33f7/EZpsxT/8pSxsKU6g Gr8BAmK8KrZ3JKfD2Lm5E9uQBCZ8OGqzMj35WvtGG X-Google-Smtp-Source: AGHT+IEF32JWoD7NuOPNh4ReJWu7Dd1sygoK2kGQaJ/mtlpLL9FTxNt2vOpcx8eqFujq9fYyVAuBAc/BN90V41LoXsY= X-Received: by 2002:ac5:c7b7:0:b0:4cd:b718:4b08 with SMTP id d23-20020ac5c7b7000000b004cdb7184b08mr10205185vkn.11.1710886811465; Tue, 19 Mar 2024 15:20:11 -0700 (PDT) Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net MIME-Version: 1.0 References: In-Reply-To: Date: Tue, 19 Mar 2024 23:20:00 +0100 Message-ID: Subject: Re: [PHP-DEV] Proposal: AS assertions To: PHP internals Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable From: tovilo.ilija@gmail.com (Ilija Tovilo) Hi Rowan On Tue, Mar 19, 2024 at 8:39=E2=80=AFPM Rowan Tommins [IMSoP] wrote: > > As well pattern matching, which Ilija mentioned, another adjacent feature= is a richer set of casting operators. Currently, we can assert that someth= ing is an int; or we can force it to be an int; but we can't easily say "ma= ke this an int if safe, but throw otherwise" or "make this an int if safe, = but substitute null/$someValue otherwise". > > I've been considering how we can improve that for a while, but not settle= d on a firm proposal - there's a lot of different versions we *could* suppo= rt, so choosing a minimal set is hard. I've thought about this in the context of pattern matching a while back. I was thinking about something like `$x is ~int`, where the pattern match is successful iff `$x` is coercible to `int` without loss of information. Given that patterns may be nested, `array<~int>` could check that all elements of an array are coercible to `int`. The same could work for literal patterns, e.g. `~5`, where `5`, `5.0` and `'5'` are all accepted. This can potentially be combined with the variable binding pattern, `$var @ pattern`. The syntax looks a bit confusing at first, but it basically makes sure that the matched value conforms to `pattern`, and then binds it to `$var`. Hence, something like `$foo as Foo { $bar @ ~int }` would 1. make sure `$foo` is an instance of `Foo`, 2. make sure `$foo->bar` is coercible to `int`, and then assigned the coerced value to `$bar`. (It gets more complicated, because the assignment must be delayed until the entire pattern matches.) If the pattern matching fails at any point, it throws. This is just an idea, neither the `as` operator nor the `~` pattern have been implemented. I don't know whether they are feasible. Anyway, we're probably going off-topic. :) Ilija