rfc:arithmetic_operator_type_checks

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
rfc:arithmetic_operator_type_checks [2020/04/01 13:54] – created nikicrfc:arithmetic_operator_type_checks [2020/05/05 14:12] (current) nikic
Line 1: Line 1:
 ====== PHP RFC: Stricter type checks for arithmetic/bitwise operators ====== ====== PHP RFC: Stricter type checks for arithmetic/bitwise operators ======
-  * Date: 2020-04-01+  * Date: 2020-04-02
   * Author: Nikita Popov <nikic@php.net>   * Author: Nikita Popov <nikic@php.net>
-  * Status: Draft+  * Status: Implemented
   * Target Version: PHP 8.0   * Target Version: PHP 8.0
   * Implementation: https://github.com/php/php-src/pull/5331   * Implementation: https://github.com/php/php-src/pull/5331
Line 14: Line 14:
 <PHP> <PHP>
 var_dump([] % [42]); var_dump([] % [42]);
-// int(1)+// int(0)
 // WTF? // WTF?
 </PHP> </PHP>
Line 65: Line 65:
  
 Using an array, resource or object in an arithmetic/bitwise operation will now consistently throw, while it previously produced a non-sensical value. Using an array, resource or object in an arithmetic/bitwise operation will now consistently throw, while it previously produced a non-sensical value.
 +
 +===== Future Scope =====
 +
 +In the future, we may wish to go one step further:
 +
 +  * Make non-numeric string operands throwing. Non-numeric here means not starting with a digit (optionally preceded by whitespace). This would not apply to operators that have special behavior for strings (string increment and bitwise and/or/xor).
 +  * Make overflowing float values throwing for operators that expect an integer (''%'', ''%%<<%%'', ''%%>>%%'', ''&'', ''|'', ''^'').
 +
 +This would have the advantage of aligning the semantics with parameter type checks in coercive mode, for the types ''int'' and ''int|float'' depending on operator. The only discrepancy would be in the handling of ''null'', which is already not as strictly enforced.
 +
 +I'm leaving this potential improvement out of this RFC, because it requires more consideration regarding backwards compatibility and overall language integration.
  
 ===== Vote ===== ===== Vote =====
  
-Yes/No.+Voting started 2020-04-16 and ends 2020-04-30.
  
 +<doodle title="Add stricter type checks for arithmetic/bitwise operators?" auth="nikic" voteType="single" closed="true">
 +   * yes
 +   * no
 +</doodle>
rfc/arithmetic_operator_type_checks.1585749295.txt.gz · Last modified: 2020/04/01 13:54 by nikic