rfc:saner-array-sum-product
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
rfc:saner-array-sum-product [2023/01/17 15:02] – Wording clarification girgias | rfc:saner-array-sum-product [2023/03/07 15:43] (current) – girgias | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== PHP RFC: Saner array_(sum|product)() | ====== PHP RFC: Saner array_(sum|product)() | ||
- | * Version: 0.1 | + | * Version: 0.2 |
* Date: 2023-01-14 | * Date: 2023-01-14 | ||
* Author: George Peter Banyard, < | * Author: George Peter Banyard, < | ||
- | * Status: | + | * Status: |
* Target Version: PHP 8.3 | * Target Version: PHP 8.3 | ||
* Implementation: | * Implementation: | ||
Line 31: | Line 31: | ||
==== Behaviour of the array_reduce() variants === | ==== Behaviour of the array_reduce() variants === | ||
- | As the < | + | As the < |
< | < | ||
Line 76: | Line 76: | ||
Pathological example: | Pathological example: | ||
<PHP> | <PHP> | ||
+ | /* STDERR gets cast to 3 */ | ||
$input = [true, STDERR, new stdClass(), [], gmp_init(6)]; | $input = [true, STDERR, new stdClass(), [], gmp_init(6)]; | ||
Line 85: | Line 86: | ||
$output = array_reduce($input, | $output = array_reduce($input, | ||
var_dump($output); | var_dump($output); | ||
+ | </ | ||
+ | |||
+ | Certain FFI\CData types implements a '' | ||
+ | <PHP> | ||
+ | $x = FFI:: | ||
+ | $x[0] = 10; | ||
+ | $x[1] = 25; | ||
+ | |||
+ | $input = [$x, 1]; | ||
+ | |||
+ | $output = array_reduce($input, | ||
+ | var_dump($output); | ||
+ | /* | ||
+ | object(FFI\CData: | ||
+ | [0]=> | ||
+ | int(25) | ||
+ | } | ||
+ | */ | ||
</ | </ | ||
Line 114: | Line 133: | ||
Pathological example: | Pathological example: | ||
<PHP> | <PHP> | ||
+ | /* STDERR gets cast to 3 */ | ||
$input = [true, STDERR, new stdClass(), [], gmp_init(6)]; | $input = [true, STDERR, new stdClass(), [], gmp_init(6)]; | ||
Line 121: | Line 141: | ||
$output = array_product($input); | $output = array_product($input); | ||
var_dump($output); | var_dump($output); | ||
+ | </ | ||
+ | |||
+ | |||
+ | FFI example: | ||
+ | <PHP> | ||
+ | $x = FFI:: | ||
+ | $x[0] = 10; | ||
+ | $x[1] = 25; | ||
+ | |||
+ | $input = [$x, 1]; | ||
+ | |||
+ | $output = array_sum($input); | ||
+ | var_dump($output); | ||
</ | </ | ||
Line 127: | Line 160: | ||
The proposal is to use the same behaviour for < | The proposal is to use the same behaviour for < | ||
- | The one caveat is that if the return value would be an object (as is the case when folding over an array of < | + | The one caveat is that objects must implement a numeric cast for them to be added/ |
- | Therefore, the previous | + | Therefore, the previous |
Example with < | Example with < | ||
Line 168: | Line 201: | ||
Warning: array_product(): | Warning: array_product(): | ||
int(18) | int(18) | ||
+ | */ | ||
+ | |||
+ | </ | ||
+ | FFI example: | ||
+ | <PHP> | ||
+ | $x = FFI:: | ||
+ | $x[0] = 10; | ||
+ | $x[1] = 25; | ||
+ | |||
+ | $input = [$x, 1]; | ||
+ | |||
+ | $output = array_sum($input); | ||
+ | var_dump($output); | ||
+ | /* | ||
+ | |||
+ | Warning: array_sum(): | ||
+ | int(1) | ||
*/ | */ | ||
</ | </ | ||
Line 175: | Line 225: | ||
< | < | ||
- | |||
- | If traversing the array transforms the return value into an object, if that object is not numerically castable an < | ||
Arrays that contain objects may now produce different results, for example: | Arrays that contain objects may now produce different results, for example: | ||
Line 185: | Line 233: | ||
Currently, results in: '' | Currently, results in: '' | ||
<PHP> | <PHP> | ||
- | Deprecated: Implicit conversion from float 25.6 to int loses precision in %s on line %d | + | int(50.6) |
- | int(50) | + | |
</ | </ | ||
Line 198: | Line 245: | ||
As per the voting RFC a yes/no vote with a 2/3 majority is needed for this proposal to be accepted. | As per the voting RFC a yes/no vote with a 2/3 majority is needed for this proposal to be accepted. | ||
- | Voting started on 2023-XX-XX and will end on 2023-XX-XX. | + | Voting started on 2023-02-20 and will end on 2023-03-06. |
<doodle title=" | <doodle title=" | ||
* Yes | * Yes | ||
Line 207: | Line 254: | ||
GitHub pull request: https:// | GitHub pull request: https:// | ||
+ | |||
+ | Landed in PHP 8.3: | ||
+ | Implemented via commit: https:// | ||
After the project is implemented, | After the project is implemented, |
rfc/saner-array-sum-product.1673967759.txt.gz · Last modified: 2023/01/17 15:02 by girgias