Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:119380 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 16317 invoked from network); 20 Jan 2023 17:43:38 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 20 Jan 2023 17:43:38 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 6FF7818054C for ; Fri, 20 Jan 2023 09:43:37 -0800 (PST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,HTML_MESSAGE, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,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-pj1-f51.google.com (mail-pj1-f51.google.com [209.85.216.51]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Fri, 20 Jan 2023 09:43:37 -0800 (PST) Received: by mail-pj1-f51.google.com with SMTP id m3-20020a17090a414300b00229ef93c5b0so4906281pjg.2 for ; Fri, 20 Jan 2023 09:43:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=G4INON4/numltn5lWV0Ln/54x5UZ7U9EAeVshkTLYJk=; b=cwcTS+Iwu0JT7bKZvsnkoeEb4npivY7U/mPQosm0RRJzkVTaMV0W1hMP/Lby73dg6I 6V+QKuFInRnqaXFy6xnFsqOL75yGSUWiaMZbnb4UMNrYNvN3fRRk28HA6ov7CrT9PVvq m7Vq318y/jYDibEjtXfhuqhp1H72fgvbi1UcLpxLopASHrKs9KQApAmAdcBCp5EhUEi/ eKconTYzfiAL17bJkx53KLIdsnziGpVWVpKeglYVQxkfsKePtUBTVrvlpA7OmNBpyiDm OrLC3YOnqLXv4Ha3QeI1WqU5fdGVUltkUV/RCOxGvPMGPZB9NdkCcqvJXYH+0w1V35bW xfFA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=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=G4INON4/numltn5lWV0Ln/54x5UZ7U9EAeVshkTLYJk=; b=lmj36kY6zobDkDXoEPanOkA7pTt3I2h+4XfgZp/O5/FoGA9yLoCLqZzFsRZ/DMXyHe f5O9l4NBXOICID9Jx9VdsnqVpwIKiuMG83QUgdDA5PY55845Hib7CZzBdzPdOxPE6MSc Kozi8YbltGoJvmlbysR4LrSY5bCAWO7NvrhsdrW2mMQTxNC4tkr8LEikVs1oMnaGgpMa 7JJqsq47PnVBYeUyI84om9LM12Wbz/jXg4n635hookU/LgpYsIHkUikE3lN9PSc8Hjwe Q8lW0ZTM+1tv3sCCmt8g0TFYbfDY7xpVKtUiEwFWdX4Bk7HK/R6bFMxG363+sTtYNFxK 1HAw== X-Gm-Message-State: AFqh2krEM8DfkTO3ePMkORu5HINGuXvp+1S6CQsyiJmSHikNqY9TASxw PRjSX0PXS2+JrGCNczY03ix8n21lei9isP2JZkFjqbLgn8Q= X-Google-Smtp-Source: AMrXdXuIWerbo1W5I/Wcu9wtZyNqp8a7TDJylz8FmH5oxTiRLGAPwj3euAqG/QhzjEHP1++Z6vfzGTDZJAzxI3LuSro= X-Received: by 2002:a17:90a:d344:b0:219:3ced:5d3c with SMTP id i4-20020a17090ad34400b002193ced5d3cmr1768715pjx.34.1674236615989; Fri, 20 Jan 2023 09:43:35 -0800 (PST) MIME-Version: 1.0 References: <5FC8DBAF-8D78-4E94-A3B0-3BDED3A3E53C@craigfrancis.co.uk> <789af205-4582-66a9-694a-10e18b8b9f56@demon-angel.eu> <9d225219-4e31-b362-52a9-9a298a0a55ef@telia.com> <768de46c-9ff2-eee2-24ab-e78e27ad167c@demon-angel.eu> In-Reply-To: <768de46c-9ff2-eee2-24ab-e78e27ad167c@demon-angel.eu> Date: Fri, 20 Jan 2023 17:43:24 +0000 Message-ID: To: Mark Baker Cc: internals@lists.php.net Content-Type: multipart/alternative; boundary="000000000000c41d6005f2b596fa" Subject: Re: [PHP-DEV] [RFC] Path to Saner Increment/Decrement operators From: george.banyard@gmail.com ("G. P. B.") --000000000000c41d6005f2b596fa Content-Type: text/plain; charset="UTF-8" On Thu, 19 Jan 2023 at 00:23, Mark Baker wrote: > > On 18/01/2023 21:25, G. P. B. wrote: > >> I would like to start the discussion on the Path to Saner > >> Increment/Decrement operators RFC: > >> https://wiki.php.net/rfc/saner-inc-dec-operators > >> > >> The goal of this RFC is to reduce language complexity by making $v++ > >> behave like $v += 1 and $v-- behave like $v -= 1; > > If that is the goal, then I would agree with this RFC. > > > > However, changing the PERL string increment feature does not IMO fit > > into that goal, and it also a useful*feature*. On that base I would > > vote against this. And I suspect many others would as well. > > However, the ++ and -- are the "Increment" and "Decrement" operators, > not the Add1 and Subtract1 operators; while they behave in that way when > used with variables containing numeric values, they are special > operators and not simply a syntactic sugar for +=1 and -=1. As long as > their behaviour is consistent, and definition of what "Increment" and > "Decrement" mean is clearly defined for different datatypes, then I feel > that the PERL-style alpha string increment has enough valid use cases to > justify itself. > That's a strange hill to die on, most people would expect that those operators do indeed behave like Add1 and Sub1, and clearly you are not having any issue with making -- act like Sub1. There is even a user note on the manual page from 21y ago where someone was expecting booleans to be incremented. [1] Moreover, the alphanumeric string increment feature is fundamentally broken and unsound due to the simple fact that PHP supports converting numeric strings written in scientific notation to float. And this behaviour of casting a numeric string in scientific notation *always* takes precedent over the alphanumeric increment. The following code samples show this perfectly: $s = "5d9"; var_dump(++$s); // string(3) "5e0" var_dump(++$s); // float(6) $s = "5e9"; var_dump(++$s); // float(5000000001) var_dump(++$s); // float(5000000002) Behaviour that *also* has a user note on the manual page. [2] It is possible to have a sound implementation of it in userland, via the following function: function polyfill(string $s): string { if (is_numeric($s)) { $offset = stripos($s, 'e'); if ($offset !== false) { /* Using increment operator would cast the string to float * Therefore we manually increment it to convert it to an "f"/"F" that doesn't get affected */ $c = $s[$offset]; $c++; $s[$offset] = $c; $s++; $s[$offset] = match ($s[$offset]) { 'f' => 'e', 'F' => 'E', 'g' => 'f', 'G' => 'F', }; return $s; } } return ++$s; } > We might also discuss consistency of datatype changes when these > operators are used. > > $a = PHP_INT_MAX; > > ++$a; > > or > > $a = '10'; > > ++$a; > > both change the datatype of $a; which isn't documented behaviour either. > Those are just "regular" well documented type coercions due to addition and int to float promotions. And yes, the PHP documentation could be better, but no one is paid to work on it. However, it turns out that I only work part-time for The PHP Foundation, and therefore I am available to be hired to do some technical writing for the PHP Documentation. (If anyone does want to take me up on this offer, do feel free to email me). In any case, I've updated the RFC to version 0.3, [3] with a whole section about the current behaviour of the PERL increment implementation in PHP, a native implementation of str_increment() and str_decrement() that handles strings that can be interpreted as numbers written in scientific notation. It also changes the timeline slightly, as this seems the preferred course of action. Best regards, George P. Banyard [1] https://www.php.net/manual/en/language.operators.increment.php#16149 [2] https://www.php.net/manual/en/language.operators.increment.php#109621 [3] https://wiki.php.net/rfc/saner-inc-dec-operators --000000000000c41d6005f2b596fa--