Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:119780 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 78130 invoked from network); 29 Mar 2023 17:43:23 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 29 Mar 2023 17:43:23 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id D11D61804F8 for ; Wed, 29 Mar 2023 10:43:22 -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,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-ed1-f54.google.com (mail-ed1-f54.google.com [209.85.208.54]) (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 ; Wed, 29 Mar 2023 10:43:22 -0700 (PDT) Received: by mail-ed1-f54.google.com with SMTP id ek18so66536122edb.6 for ; Wed, 29 Mar 2023 10:43:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1680111801; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=pwoyu1mIpXYe3Kd4Ds0YoQR2D1OAj42CJTuJuuYH1Og=; b=k6lbfs/oEaR5R9K/0R2qSwVIhgoC/Ny3JuwIwtCJVtThSchNUbvuncVnJugn0ZRwPh TmQDnyuTdQv6kKDRC1DQBa0GbBZQvYpJXGJGKHiz+yj9oMXLYesShVdX4tqdNU5zF2QN D8nL0bCsNASFSVjlbYT4nPJpAt/5yPO7Jfo7dWyzJ5b/eiFGYrRXRsy67YXQVhDxPqV6 8I8BIzL8Pqs0cpL4jyp32hTGrtfZH5R54JDBHT1AS4TmLFIhvhDgggcmnZEp7jw1ITl2 kpzODBc702y5+xVdS8tcNn5KxxOvrX8Mtu5hFAVoTaV3R/GtIzVmml9tYM2QwFiWafgJ RLEQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680111801; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=pwoyu1mIpXYe3Kd4Ds0YoQR2D1OAj42CJTuJuuYH1Og=; b=7GQdRUduFltjTDMC9crNA8Z8anYVMnfh9NtA7Rx4lsYiqEHoQuZ4xMO5iF3ndx8xBX 7cxb7u6rmN06PEzXQrpPHWEObhiebggeAbCqSfiaFq6lFMgkSdj3QF5PMYsJH2PYdkzo hhEOyJMXdtzLF0x+gwmq33mIxpbL5cA5vUYB9qM1XNLI5S8FG7wdR8KLHnsTZ2J8JGqr 9PJp4oxfCJeCs5882p5oY9Meu4Tg9yoWykic8qJ1OR4/NtsttDjfwg6XS++84dZApgMI QPZhJi6QV6FXkAfWIO+ehqgLLJmAawBmtGW4BtB2qqf+VRfhCfnooxd5oUUSg6I1jo0a oDqA== X-Gm-Message-State: AAQBX9flOcpI6yOYBoJ/3+2R3Wn3jqezVQE16r0Z69FziJwzc5nSQA+h wgAjUZGGSoBKsesxdWECn00= X-Google-Smtp-Source: AKy350Y8WDCsR3zxsPdVHLqsyugLpfPPAUNH0bkLlmJB7f73OFwU789DW6QMkL7teiNio60vXU1FHA== X-Received: by 2002:aa7:d885:0:b0:4fd:2155:74ef with SMTP id u5-20020aa7d885000000b004fd215574efmr20355316edq.19.1680111800913; Wed, 29 Mar 2023 10:43:20 -0700 (PDT) Received: from ?IPV6:2a02:1811:cc83:eef0:7bf1:a0f8:a9aa:ac98? (ptr-dtfv0pmq82wc9dcpm6w.18120a2.ip6.access.telenet.be. [2a02:1811:cc83:eef0:7bf1:a0f8:a9aa:ac98]) by smtp.gmail.com with ESMTPSA id v30-20020a50a45e000000b005021d17d896sm7856330edb.21.2023.03.29.10.43.20 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 29 Mar 2023 10:43:20 -0700 (PDT) Message-ID: Date: Wed, 29 Mar 2023 19:43:19 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.9.0 To: "G. P. B." , Christian Schneider Cc: PHP internals References: <9318094F-139C-4392-8307-1C42B327CDD6@cschneid.com> Content-Language: en-US In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] [RFC] Define proper semantics for range() function From: dossche.niels@gmail.com (Niels Dossche) Hi On 28/03/2023 14:42, G. P. B. wrote: > 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 > I like the RFC in general, just this negative $step parameter warning stood out to me. While I agree that requiring a negative $step for a decreasing range is a bit silly, I've always found it intuitive that a negative $step should be used for a decreasing range. I'm not saying that it should be required, I'm just concerned about the BC break of emitting E_WARNING and breaking people's intuition. The reasoning behind this is that range($start, $end, $step) (for me at least) has always been symmetric and consistent with a for loop: $array = []; for ($i = $start; $i < $end (or >); $i += $step) { $array[] = $i; } So by allowing a negative step the behaviour is consistent with how for loops behave, and avoids a BC break. That Kotlin doesn't require it makes sense for me because the words "down to" combined with a negative step indeed don't make sense. The negativity is in a sense already embedded by explicitly writing "down to". It's less explicit for PHP's range function. I looked at the source code of some of my projects and I do see some occurrences of range($high, $low, -1 (or another negative value)). Just wanted to chime in quickly to state my thoughts on this. Thanks. Kind regards Niels