Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:119756 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 72016 invoked from network); 28 Mar 2023 16:03:50 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 28 Mar 2023 16:03:50 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 3F488180507 for ; Tue, 28 Mar 2023 09:03:49 -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=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS15623 212.45.192.0/19 X-Spam-Virus: No X-Envelope-From: Received: from mail.gna.ch (mail.gna.ch [212.45.196.109]) (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 ; Tue, 28 Mar 2023 09:03:48 -0700 (PDT) Received: from localhost (localhost [127.0.0.1]) by darkcity.gna.ch (Postfix) with ESMTP id 223D03A1DCD for ; Tue, 28 Mar 2023 18:03:47 +0200 (CEST) X-Virus-Scanned: amavisd-new at example.com Received: from mail.gna.ch ([127.0.0.1]) by localhost (darkcity.gna.ch [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id lQoSzmQ0VMg9 for ; Tue, 28 Mar 2023 18:03:46 +0200 (CEST) Received: from smtpclient.apple (unknown [IPv6:2a02:1210:2ea4:cf00:d5f5:91d7:c707:e14c]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by darkcity.gna.ch (Postfix) with ESMTPSA id A743F3A1DC4 for ; Tue, 28 Mar 2023 18:03:46 +0200 (CEST) Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: quoted-printable Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3731.400.51.1.1\)) Date: Tue, 28 Mar 2023 18:03:46 +0200 References: <9318094F-139C-4392-8307-1C42B327CDD6@cschneid.com> To: PHP internals In-Reply-To: Message-ID: <6D741F34-AE0C-4FBD-9EF1-726468FB40C3@cschneid.com> X-Mailer: Apple Mail (2.3731.400.51.1.1) Subject: Re: [PHP-DEV] [RFC] Define proper semantics for range() function From: cschneid@cschneid.com (Christian Schneider) Am 28.03.2023 um 14:42 schrieb G. P. B. : > On Tue, 28 Mar 2023 at 08:19, Christian Schneider = wrote: >> There are two cases I would handle differently: >> - I'm not sure why a negative step for $start > $end is considered = wrong, I consider range(10, 0, -2) at least as logical/readable as using = a positive decrement of 2. Not requiring a sign for steps seems weirder = to me but that's something we cannot change. *BUT* if it is the result = of a calculation it seems wrong to require an abs() around it. I do see = the reason for a warning/error when $start < $end and $step < 0. >>=20 > Considering the only other programming language that I know of that = has a range() function that accepts a step argument is Python, and its = behaviour is IMHO worse. > For increasing ranges it requires a positive step, and if not just = generates an empty range. For decreasing ranges it requires a negative = step, and if not just generates an empty range (this applies even if = using the default step value of 1 which is bonkers). >=20 > Making it a requirement to pass a negative step is definitely out of = the question. > Making it okay to use negative steps *only* for decreasing ranges = could be sensible, but we check for the step parameter way before we = look into the boundary values because those are different for int, float = and string boundaries. > Moreover, I personally find it weirder to require a sign for negative = steps as for me a step is something that *must* be positive I quickly checked our own codebase and there is indeed one instance of range($last, 0, -1) which was not written by me so there is at least one more person who = found this logical ;-) >> - Values of '' or null in integer context (e.g. range(null, 10, 2)) = should IMHO emit a warning first, not directly be changed to a = TypeError. The usual BC / migration concern :-) >=20 > Trying to figure out if an empty string was used with another string = boundary is tedious, as this information needs to somehow get carried = around. > A previous iteration of the PR used to convert empty strings to 0 with = a warning, but considering the analysis I decide to just make this a = ValueError as it doesn't seem that empty strings are actually used in = practice. > But this is an easy revert, and I'm not really bound to this decision. Even though this might be a bit cumbersome at the moment I think this = would be an important transition step as currently range('', 10, 2); returns the (somewhat) expected result of 0, 2, 4, 6, 8, 10 so I'd be in = favor of first giving a warning and then changing it to a ValueError. - Chris