Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:119753 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 57368 invoked from network); 28 Mar 2023 12:42:44 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 28 Mar 2023 12:42:44 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 8B2CE180505 for ; Tue, 28 Mar 2023 05:42:43 -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=-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_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-pf1-f176.google.com (mail-pf1-f176.google.com [209.85.210.176]) (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 05:42:43 -0700 (PDT) Received: by mail-pf1-f176.google.com with SMTP id fd25so7891672pfb.1 for ; Tue, 28 Mar 2023 05:42:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1680007361; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=61BBbU+jEQCYlJsO1hp2NFQ/AYbRByfX5UpoABQbX7c=; b=FyydteSoGkHuzwdONLG+0On29RxyHGbbpvXIM9E7O3lOh6y43l9m0OAc1ti9nK++WI YSCNlKl0mzh4ATAKqaWPQ7KdrDMJP2Vb29JH5ebpz8kKIpi2yX2O4D//Xoz3rUqk5vAH Sp7w/28omeTV2bCkw1Qwn8UC6XrZxIAgMgKOVGSnvme1LwvKhSoFUnBmJoDEfM0e+yTL STDF1nnFlpwh6hqBYOt5ufXIkwwHhbNkdtXOPixQdcRgmB67DdQYg5BctoNKoMrcg0re rkgiISdUi8tdABC1tbDN+4DofazzfMU1Qq7BWJrAyC/QY7lZ9SvbE0trrN/TVIqMV2bA 1GhA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680007361; 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=61BBbU+jEQCYlJsO1hp2NFQ/AYbRByfX5UpoABQbX7c=; b=7oMJ7gI8q3lt+7Yj7GFHYEgYItLGFvQj+wvR2hjQVFUlspzIi4Pa9oNgeLrOwoUUZe alweDmPkr1cZcT/m/xN6I9NgM+bN9uS6vKjrq5HQNFjON1h4MBdUI8RybfCAzUbVccBT M7kgX0MTUv6gezj1vfsGmEYpxqfMP3VJvJO+6fLzfq/28DpsRv3K6GsNnvjiyuw7j9ZS wAqjpwV0W+so7eY4rWuxWMYpYwMqz2Om2ASlWiiglEg27UF3muvA9R4h2+SedY7XMeGb c9cxdSOd35mk8RMe7y0We35qryGh0Th+l/NDGi3H/61ySagBGfZJ6Cfu4tp3Ev6bGqeR 6eTw== X-Gm-Message-State: AAQBX9djs/OM/n3MvB9S81qf8SPx648PRUbAwez0MN6jtyHE0Cak+QDO 4kKEMm6jQSiNW8xrs5cdQIzBq5AB24GeFAoOqAGzTdCoBP0= X-Google-Smtp-Source: AKy350ZGi8BbLwWyOuNYMZbfPStOGA3FC22p1eCUOg55/0YvzKaTby1Vc35jTBypbE4FY43L6VKm1tJtVu7FipI8gTU= X-Received: by 2002:a63:1424:0:b0:503:4a2c:5f0 with SMTP id u36-20020a631424000000b005034a2c05f0mr4004354pgl.9.1680007361429; Tue, 28 Mar 2023 05:42:41 -0700 (PDT) MIME-Version: 1.0 References: <9318094F-139C-4392-8307-1C42B327CDD6@cschneid.com> In-Reply-To: <9318094F-139C-4392-8307-1C42B327CDD6@cschneid.com> Date: Tue, 28 Mar 2023 13:42:30 +0100 Message-ID: To: Christian Schneider Cc: PHP internals Content-Type: multipart/alternative; boundary="000000000000ff85fa05f7f531a5" Subject: Re: [PHP-DEV] [RFC] Define proper semantics for range() function From: george.banyard@gmail.com ("G. P. B.") --000000000000ff85fa05f7f531a5 Content-Type: text/plain; charset="UTF-8" On Tue, 28 Mar 2023 at 08:19, Christian Schneider wrote: > Am 28.03.2023 um 00:36 schrieb G. P. B. : > > I therefore propose the "Define proper semantics for range() function" > RFC > > to address the unintuitive behaviour that sees no usage and/or hide bugs: > > https://wiki.php.net/rfc/proper-range-semantics > > > > The change propose to throw TypeErrors and ValueErrors for case where I > > couldn't find occurrences in the wild and hide bugs, and emit some > > E_WARNINGs for cases that are hard to detect via static analysis. > > I think it makes sense to clean up the range() function, thanks! > > 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. > 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). 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, and at least Kotlin seems to somewhat agree with me looking around at https://kotlinlang.org/docs/ranges.html#progression and playing with the source code Namely: for (i in 4 downTo 1 step 2) print(i) > 42 for (i in 4 downTo 1 step -2) print(i) > Exception in thread "main" java.lang.IllegalArgumentException: Step must be positive, was: -2. > - 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 :-) > When null is used, no TypeError is emitted, just the "usual" null to scalar deprecation that happens since https://wiki.php.net/rfc/deprecate_null_to_scalar_internal_arg got accepted. But I'll add an example to the RFC and a test case in the PR. 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. Best regards, George P. Banyard --000000000000ff85fa05f7f531a5--