Re: [RFC] Strict operators directive
| From: | Benjamin Morel | Date: | Wed, 26 Jun 2019 11:46:03 +0000 |
| Subject: | Re: [RFC] Strict operators directive | ||
| References: | 1 2 3 4 5 6 7 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-106073@lists.php.net to get a copy of this message | ||
> * in some strict mode, reserve
<,
> == etc. for numeric comparison, and
throw a TypeError one of the operand is not numeric;
> * If we deem it worth, define a new operators for string comparison.
(Although I’m not really sure it is worth: we have strcmp() and === for
byte-to-byte comparison, and the Collator class for alphabetical sorting
that actually works in languages not restricted to unaccented latin
characters.)
It's true that string comparison (sorting) is a much harder problem that
cannot be solved without additional knowledge of the encoding of the
string; so I agree that it might be better to just throw a TypeError when
comparing strings, and leave the user with an operator that only works on
numbers, and explicitly use dedicated functions when comparing strings.
This makes sense for "<", "<=", ">", ">=", but
what about "==" and "!="?
Currently, "11" == "11.0"; what would this yield under the new proposal?
- leave it as is: return true in this case => contradicts the whole purpose
of the new proposal
- throw a TypeError when performing the above comparison => not acceptable
either I guess; every language allows == and != on strings, forcing to use
strict comparison operators is a bit weird here.
- change the semantics to return false when both operands are strings, and
don't match => not acceptable to you as you cannot know what a line of code
does without checking the header
What would you suggest here?
Ben