Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:123731 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 EEE201A009C for ; Fri, 21 Jun 2024 14:21:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1718979794; bh=up4MxEpk6Oo0HISLv3y5vM+ioXRsqQSSRTvqac2Huxc=; h=Date:Subject:To:References:From:In-Reply-To:From; b=me7F9IK7ukRdiXveMpuakS7Ef99sxYQ7i9TFw3MpNmeO+SnV/+NO/PfJwPA5pJ+t8 tFK70GXcJsn41Hmfd714tmNw+7jkyaVpgtyLF8LdDJNCiDpPo39ewxYHcg91SpbGXz a5UlLU4MmwXKZcbqRrZ2STUi6Ei6/SRZijUD7OrCxQanUdQ/RUfukfhU6W/Tqk9KEt hpWIF0UMhqKeCGHs34vN+13EO91FeTiuI2qB8Pmuj55qhYkEXXavocA5+h82p2O//Y uyswkf/KAkRuVjcgqxSovJuBuZSdZGmJY6btFKca5eCSmRUXv5O3OFhNRWZj8ZgOiA NM4krrHxoAt4Q== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 9F103180050 for ; Fri, 21 Jun 2024 14:23:12 +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, RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,SPF_HELO_PASS,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 smtp-42a9.mail.infomaniak.ch (smtp-42a9.mail.infomaniak.ch [84.16.66.169]) (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 14:23:12 +0000 (UTC) Received: from smtp-3-0001.mail.infomaniak.ch (smtp-3-0001.mail.infomaniak.ch [10.4.36.108]) by smtp-4-3000.mail.infomaniak.ch (Postfix) with ESMTPS id 4W5KLC6XD7z2X6 for ; Fri, 21 Jun 2024 16:21:55 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=processus.org; s=20231208; t=1718979715; bh=euYOu4zNK635lumRQ2XmPx+7tpukG0VqFMGxRmtnCcw=; h=Date:Subject:To:References:From:In-Reply-To:From; b=XZqK9NFKsJnXgrlztpTS5dIQFE6YS3BlYs053ft8cCezIlRpCnQy3TadT9v1L82lD ZAwAYACHQ1CDfez6EjZ/3b1CVm0xQOt5ibddBaL2WlbywgzUJtrRNVMiQSKcBtMyiX 7IxU0N6OQx4i30JdCtyLXZYFtdovaOZsDVloEy+bPE0iGObtR9PHHYE/mfbGxri18/ rBbq9r9wRev2y19nj5UcC44zATvbTlYDCRGj5V3qOXb/Az6yT7ITVc2eg2DYlB2+4R hhmaztSlEuGa1ZJb8ngILA8QFizltyzdCOTfCHhoDwercFKyE35o9PZnS6ELw2x3rJ YKMb03ieC6uwA== Received: from unknown by smtp-3-0001.mail.infomaniak.ch (Postfix) with ESMTPA id 4W5KLC48ySz7T1 for ; Fri, 21 Jun 2024 16:21:55 +0200 (CEST) Message-ID: <84be04f2-73cd-4fdb-929d-724d4fea98fa@processus.org> Date: Fri, 21 Jun 2024 16:21:55 +0200 Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PHP-DEV] Renaming "strict types" to "scalar type coercion" To: internals@lists.php.net References: <9d28c9tOCChpWUcP1SFiFwtf2-w2XAtDzHsmM73myA7zfBsn7TxxEqs-VCnxRAgZjsk2TxS55owmM8wSLgBQL8LgeQLntUQ9toeLMkjTVx4=@gpb.moe> <673a1803-83c4-44b4-bd73-4ba61e83412e@app.fastmail.com> <350c1531-9bcf-4e7c-824b-8d9904b9e48e@rwec.co.uk> <914cde73-3e53-44a8-94ff-bcf34000cc0c@processus.org> Content-Language: fr-FR, en-US In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Infomaniak-Routing: alpha From: pierre-php@processus.org (Pierre) Le 21/06/2024 à 15:57, Robert Landers a écrit : > On Fri, Jun 21, 2024 at 3:01 PM Pierre wrote: >> Le 21/06/2024 à 14:27, Robert Landers a écrit : >> >> This is why I wanted to work on "as" part of the pattern matching. It >> isn't clear what will happen with the actual pattern matching RFC >> (yet), but being able to do: >> >> some_function_expecting_int($_GET['foo'] as ?int); >> >> And how about: >> >> some_function_expecting_int(\intval($_GET['foo'])); >> >> And moreover, I'd write something like this, but: >> >> function validate_int(mixed $value): int { >> if (null === $value || '' === $value) { >> return null; >> } >> if (\is_int($value)) { >> return $value; >> } >> if (\is_string($value) && \ctype_digit($value)) { >> return \intval($value); >> } >> throw new \InvalidArgumentException("What what!"); >> } >> >> some_function_expecting_int(validate_int($_GET['foo'] ?? null)); >> >> But the example might be erroneous, I see your point, nevertheless making coercion explicit doesn't seem really relevant to me, the one point I like in your syntax would be null handling. >> >> -- >> >> Pierre > Or... you could just turn off strict types instead of reinventing > coercion that isn't nearly as well documented as the built-in > coercion. That was one of my points actually. I don't see the use of a syntax such as `$foo as ?int` since all existing types of coercion are already possible via the cast syntax (please correct me if I'm wrong). In the previous message proposal, the only neat addition was the null handling (that's said, that's a really cool point). But cast syntax `(int) $foo` is really equivalent to `$foo as int` in terme of concision (less than 2 chars diff). In almost every case you would need coercion you also need validation, and validation is not something that's possible with a neat syntax, at least not this one, because validation is a business matter, not really a technical one. Regards, -- Pierre