Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:106207 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 13116 invoked from network); 11 Jul 2019 00:17:51 -0000 Received: from unknown (HELO mail-oi1-f172.google.com) (209.85.167.172) by pb1.pair.com with SMTP; 11 Jul 2019 00:17:51 -0000 Received: by mail-oi1-f172.google.com with SMTP id 65so2810045oid.13 for ; Wed, 10 Jul 2019 14:37:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to; bh=rwaiHiinpONQcHXdoA9zOimfks7vMNDpArh3ThiE/9o=; b=qx+afUje0wUyL8ULaPiDxtLqmOj5Iu8+6YKY9Ok2UQQeYzRUmm81VEAoRJX0Gjk1bL uMMGGtp14zwRE39+PIeXpDR7ZzUZIwKR4sS+RECDmkTAuBBPIRx54auIqECaSRWc9Thr SFcDUSFm9tYSlr20dA1iKoj+yjERDtTh8nWmJJLaS68uiL4AmMuxjjWz6YSRuTlwcB+W wAPTE6Nlkk9PXaEPSvNjYLULhu6Sqct0Z3aZUvKv/AjdrkgMpwJOfDpyxg4BciPtn9T4 Xc4IgGGd1002RoT2B0GEGt+ivQ208oPdc0saN3w9BGq95pHTTzpOvew6NmYFunKAv4pq szBQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to; bh=rwaiHiinpONQcHXdoA9zOimfks7vMNDpArh3ThiE/9o=; b=otcgEzzGdLr0jWD2OX+8LJk/4ql1U6yfA/16nUJRkORdrJcdc8DdzQIdJbPa8E6/FY QaUbt8kC8we6E5Z+TA0sICXXC/fAS6aw2LdTQigLyQjxuK2hlDCJDUiL0V3sumuL0hR7 v7p8ZeSqCgwU1b6tUx3vK059XVmyNgMKc8hUzhA9xYiymZLNbkeOnsP/XzBfgpiVec// XKKe79/hsmK2BYWJhDF0gAuOZspGLX+Yfsgn/FDwMHYlj2iuPXxKL3nyNe4SZaQuzN0i 2y1WIGa7mGJXvWkjdW/DNE0jVpbqi+p2sGlo0EBZZbMa9PxqNS/mhCId0BsIeMwxW1y6 yLEg== X-Gm-Message-State: APjAAAXvBULZZhfPZq90UUjJaf+XBMGuymAxcYzNdglo9F1cRTettsPB xAmAN47mKf5umK4kGHzOgqnL627xtxFdm3SmA2WQCuxKRxQ= X-Google-Smtp-Source: APXvYqzQt9Om9TLCf2bx7y82P6/f8vK8eh92h5sUuIud2YmhGYD3vtBd6w7Non3R1o9vfGYVr8gH7sVfiITT50nheEA= X-Received: by 2002:a05:6808:a04:: with SMTP id n4mr336133oij.40.1562794670322; Wed, 10 Jul 2019 14:37:50 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Wed, 10 Jul 2019 23:37:38 +0200 Message-ID: To: PHP Internals Content-Type: multipart/alternative; boundary="0000000000002e77db058d5a7d60" Subject: Re: [PHP-DEV] [RFC] Strict operators directive From: arnold.adaniels.nl@gmail.com (Arnold Daniels) --0000000000002e77db058d5a7d60 Content-Type: text/plain; charset="UTF-8" Hi Nikita, Thanks for your feedback. I'll fix the textual errors you mentioned. * "To compare two numeric strings as numbers, they need to be cast to > floats." This may loose precision for integers. It is better to cast to > numbers (int or float) using, with the canonical way being +$x. But I guess > that won't work under strict_operators. Maybe we should have a (number) > cast (it already exists internally...) > Good point. While in most cases you know if you're working with floats or integers, adding a way to cast to either an int or float would be nice. Maybe preferably through a function like `numberval($x)` or simply `number($x), so the `(type)` syntax is reserved for actual types. That would be an RFC on its own though. > * This has already been mentioned by others: Having $str1 < $str2 perform > a strcmp() style comparison under strict_operators is surprising. I think > that overall the use of lexicographical string comparisons is quite rare > and should be performed using an explicit strcmp() call. More likely than > not, writing $str1 < $str2 is a bug and should generate a TypeError. Of > course, equality comparisons like $str1 == $str2 should still work, similar > to the distinction you make for arrays. > Ok, fair. I'll change it so <,<=,>,>=,<=> comparison on a string throws a TypeError, similar to arrays, resources, and objects. > * If I understand correctly, under this RFC "foo" == 0 will throw a > TypeError, but ["foo"] == [0] will return false. Generally the behavior of > the recursive comparison here is that it's the same as strict == but all > errors become not-equal instead. Correct? I'm not sure how I feel about > this, as it seems to introduce one more set of semantics next to the weak > ==, strict == and === semantics there already are. > The syntax would be `$a == $b` (or `$a == [0]`), where $a and $b are a string/int in one case and both an array in the other case. In the second case, we can't throw a TypeError as both operands are of the same type. > * I also find it somewhat odd that you can't write something like "$obj != > null" anymore, only "$obj !== null". > To check against null, it's better to use !==. For objects (and resources) using `!= null` is ok, but for other types, it's currently not. For example; `[] == null` gives true. > * I think the "solution" to the last three points is a) only support > numbers in relational operators (<,<=,>,>=,<=>) and throw TypeErrors > otherwise (maybe modulo provisions for object overloading) and b) allow > comparing any types in == and !=, without throwing a TypeError. The > question "Are 42 and 'foobar' equal?" has a pretty clear answer: "No they > aren't", so there is no need to make this a TypeError (while the question > "Is 42 larger than 'foobar'?" has no good answer.) I believe doing > something like this would roughly match how Python 3 works. (Edit: I see > now that this is mentioned in the FAQ, but I think it would be good to > reconsider this. It would solve most of my problems with this proposal.) > Besides the argument in the FAQ, having the == and != return do a type check, means there are a lot more cases where the behavior changes rather than that a TypeError is thrown. Currently `"foobar" == 0` returns true, but this would make it return false. So would `1 == true`, `"0" == 0` and `"0" == false`. To reduce the cases where the behavior changes to a minimum, it's better to throw TypeErrors for == and !=. > * String increment seems like a pretty niche use case, and I believe that > many people find the overflow behavior quite surprising. I think it may be > better to forbid string increment under strict_operators. > Ok > * A similar argument can be made for the use of &, | and ^ on strings. > While I have some personal fondness for these, in practical terms these are > rarely used and may be indicative of a bug. I think both for string > increment and string and/or/xor it may be better to expose these as > functions so their use is more explicit. > These operators make it very easy to work with binary data as strings in PHP. In other languages you have to work with byte arrays, which is a major pain. They're also very intuitive; `"wow" & "xul"` is the same as `chr(ord('w') & ord('x')) . chr(ord('o') & ord('u')). chr(ord('w') & ord('l'))`. I think these should stay. > > Regards, > Nikita > Arnold --0000000000002e77db058d5a7d60--