Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:117630 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 1053 invoked from network); 26 Apr 2022 17:50:39 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 26 Apr 2022 17:50:39 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id E0C631804C6 for ; Tue, 26 Apr 2022 12:25:57 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-0.2 required=5.0 tests=BAYES_40,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,NICE_REPLY_A, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS15169 209.85.128.0/17 X-Spam-Virus: No X-Envelope-From: Received: from mail-wr1-f48.google.com (mail-wr1-f48.google.com [209.85.221.48]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Tue, 26 Apr 2022 12:25:57 -0700 (PDT) Received: by mail-wr1-f48.google.com with SMTP id v12so20023494wrv.10 for ; Tue, 26 Apr 2022 12:25:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=message-id:date:mime-version:user-agent:subject:content-language:to :references:from:in-reply-to:content-transfer-encoding; bh=ZUoAg7ZC8fyjApis2oji8nm7dk2dDGbadgmFZjlmOZk=; b=ficpQBgmMWOVP5fWHw1UF8M9ZPWicsQdeWh9L9ar4HOBsGIUGVnXlO3Ho7YGXhe/nJ uJ/Z3mCVhktNPGikn8IwsqII/yKJuOohuFjRy+ks8PPhfXKF8c9xHO1EAEqilayu0AjJ o2nunn6rRaiIfkJ+u1PfT+2nmwleX/xDzGi5uCeZ5X/bon76nps4ujBCs0Df04a0ebU1 bxs5YB7zPCJqerlzWKxy9C1IszAIhdE9+lywtxu2xK9TVHFHLdKccutzYaTnbp+nP2Jd d86MdVnadDMkcLHiiVCxxx7k6nRoER+fw42Mg3KTDu/lrKJW4nsmyTDuXSysk/zj9ydO 8zSQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :content-language:to:references:from:in-reply-to :content-transfer-encoding; bh=ZUoAg7ZC8fyjApis2oji8nm7dk2dDGbadgmFZjlmOZk=; b=epRjUxjeK4V9ePQnSE9LodZkT1FqU2swFfkd6OSZxAf61KT0mDtKk3rR3IWc3WnKfi EGS6ZS0j4l+8/IPfbXoWGkV5Pu7ZdA0oE9mvBQgQJmCp/yfKu4NO6hBOC58+x0ZMs3Cv GJHLjukUJ4fl/l1wm2BqeTJYb5ASejghFflCdi4lHvTzUAb2P5kNr+2mZg5Zj+1yywrQ lFCyHZ49QWP8vUDQo5iIkboBvtg92If+7cO5aGWEH3NHPjETwL8dql1OimBNXc6mRruM OrWdw0nrvpWduYXN4kGXewRPQcG0Z/AY5mseCNa6axOwYf+iWuzp1u8EIgEk7Edqp0mA pCpQ== X-Gm-Message-State: AOAM531LlLLuKQ0oD6LG/UjX1XtPBZIfhHUvUjxnfjGBh9KEJqe7jscx OmRdgb2yxIM1rN4In+a+OVkNGdTgCA0= X-Google-Smtp-Source: ABdhPJxYP42t+3SgNgNwSQXmsKywuEhUCpI+mXioUMZZ9G7LUYTiPYAKifPvrxKhcqNs6mbpDzlRJA== X-Received: by 2002:adf:b34a:0:b0:20a:d6e4:cea4 with SMTP id k10-20020adfb34a000000b0020ad6e4cea4mr12593821wrd.675.1651001156031; Tue, 26 Apr 2022 12:25:56 -0700 (PDT) Received: from [192.168.0.22] (cpc104104-brig22-2-0-cust548.3-3.cable.virginm.net. [82.10.58.37]) by smtp.googlemail.com with ESMTPSA id y9-20020a05600015c900b0020adb0e106asm7869840wry.93.2022.04.26.12.25.54 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 26 Apr 2022 12:25:55 -0700 (PDT) Message-ID: <73775976-c23d-0ee1-3ea2-ffb39431fc50@gmail.com> Date: Tue, 26 Apr 2022 20:25:53 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Thunderbird/91.8.0 Content-Language: en-GB To: internals@lists.php.net References: <3fcdfa2c-7a9c-d634-ea56-8b1e5bf1a911@gmx.net> <5ceeb3f0-0f01-8b93-00ca-9947c9c69e4c@gmx.net> In-Reply-To: <5ceeb3f0-0f01-8b93-00ca-9947c9c69e4c@gmx.net> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] Stricter implicit boolean coercions From: rowan.collins@gmail.com (Rowan Tommins) On 26/04/2022 14:53, Andreas Leathley wrote: > This is also something I am interested in - having functions which do > the same as implicit type casts, so something like > "coerce_to_int('hello')" would lead to a TypeError like it would when > passing it to an int parameter, and maybe > "is_coerceable_to_int('hello')" which would return false, so it would be > possible to check values the same way as PHP does internally. The > current explicit cast functions are quite heavy-handed and not great for > every situation - when parsing a CSV or getting data from a database I > would rather coerce values where an error could occur if it doesn't make > sense than to explicitely cast and not even notice if the value made no > sense at all. Yep, that's pretty much exactly where I was going. My current thinking is to have at least one of (deliberately long straw man names): type_coerce_or_return_default(string $type, mixed $value, mixed $default): mixed type_coerce_or_throw_error(string $type, mixed $value): mixed type_can_be_coerced(string $type, mixed $value): bool Accepting the type as a string is ugly, but means we don't need three functions for every type, and can easily support nullable types, union types, etc. Crucially, no special syntax means it's possible to write polyfills that compile in old PHP versions. For simple types, you can mostly get away with one function: $float_or_error = type_coerce_or_return_null('float', $var) ?? throw new TypeError; $is_valid = type_coerce_or_return_null('float', $var) !== null; But that's no use when null is a valid return type, so you need a hand-picked default and a bunch of extra boilerplate: if ( $nullable_float = type_coerce_or_return_default('?float', $var, false) === false ) { throw new TypeError; } $is_valid = type_coerce_or_return_default('null|float|bool', $var, 'invalid value') !== 'invalid value'; The main point I'm stuck on at the moment is exactly how strict these should be, since we already have more than one set of coercion rules for each type, as Mel Dafert pointed out elsewhere on this thread. On the one hand, they should probably match with at least some existing rules; on the other, it would be weird to introduce them then immediately deprecate some behaviour because we've decided to make the language stricter elsewhere. Regards, -- Rowan Tommins [IMSoP]