rfc:arithmetic_operator_type_checks

Differences

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

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
rfc:arithmetic_operator_type_checks [2020/04/02 08:03] nikicrfc:arithmetic_operator_type_checks [2020/05/05 14:12] (current) nikic
Line 2: Line 2:
   * Date: 2020-04-02   * Date: 2020-04-02
   * Author: Nikita Popov <nikic@php.net>   * Author: Nikita Popov <nikic@php.net>
-  * Status: Under Discussion+  * 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 62: Line 62:
 The changes proposed here are intended to be entirely uncontroversial. The changes proposed here are intended to be entirely uncontroversial.
  
-===== Open Questions =====+===== Backward Incompatible Changes =====
  
-It might be worthwhile to go one step further:+Using an array, resource or object in an arithmetic/bitwise operation will now consistently throw, while it previously produced a non-sensical value.
  
-  * 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, such as string increment.+===== 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 (''%'', ''%%<<%%'', ''%%>>%%'', ''&'', ''|'', ''^'').   * 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. 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.
  
-===== Backward Incompatible Changes ===== +I'm leaving this potential improvement out of this RFCbecause it requires more consideration regarding backwards compatibility and overall language integration.
- +
-Using an arrayresource or object in an arithmetic/bitwise operation will now consistently throw, while it previously produced a non-sensical value.+
  
 ===== 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.1585814638.txt.gz · Last modified: 2020/04/02 08:03 by nikic