rfc:is_list
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
rfc:is_list [2020/12/20 20:22] – tandre | rfc:is_list [2021/01/20 23:51] (current) – end vote tandre | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== PHP RFC: Add is_list(mixed $value): bool ====== | + | ====== PHP RFC: Add array_is_list(array $array): bool ====== |
- | * Version: 0.1 | + | * Version: 0.3 |
* Date: 2020-12-19 | * Date: 2020-12-19 | ||
* Author: Tyson Andre < | * Author: Tyson Andre < | ||
- | * Status: | + | * Status: |
* Implementation: | * Implementation: | ||
* First Published at: https:// | * First Published at: https:// | ||
Line 13: | Line 13: | ||
===== Proposal ===== | ===== Proposal ===== | ||
- | Add a new function '' | + | Add a new function '' |
This RFC doesn' | This RFC doesn' | ||
Line 20: | Line 20: | ||
<code php> | <code php> | ||
- | function | + | function |
- | if (!is_array($value)) { return false; } | + | |
- | | + | |
$expectedKey = 0; | $expectedKey = 0; | ||
- | foreach ($value as $i => $_) { | + | foreach ($array as $i => $_) { |
if ($i !== $expectedKey) { return false; } | if ($i !== $expectedKey) { return false; } | ||
$expectedKey++; | $expectedKey++; | ||
Line 32: | Line 30: | ||
$x = [1 => ' | $x = [1 => ' | ||
- | var_export(is_list($x)); | + | var_export(array_is_list($x)); |
unset($x[1]); | unset($x[1]); | ||
- | var_export(is_list($x)); | + | var_export(array_is_list($x)); |
// Pitfalls of simpler polyfills - NAN !== NAN | // Pitfalls of simpler polyfills - NAN !== NAN | ||
Line 41: | Line 39: | ||
var_export($x === array_values($x)); | var_export($x === array_values($x)); | ||
var_export($x); | var_export($x); | ||
- | var_export(is_list($x)); | + | var_export(array_is_list($x)); |
+ | |||
+ | array_is_list(new stdClass()); | ||
+ | array_is_list(null); | ||
</ | </ | ||
Line 57: | Line 58: | ||
- | ===== Proposed PHP Version(s) ===== | + | ===== Proposed PHP Version ===== |
8.1 | 8.1 | ||
Line 64: | Line 65: | ||
==== To Opcache ==== | ==== To Opcache ==== | ||
- | Opcache' | + | Opcache' |
- | In the RFC's implementation, | + | In the RFC's implementation, |
- | Long-term, if this sees wide enough adoption to affect performance on widely used apps or frameworks, opcache' | + | Long-term, if this sees wide enough adoption to affect performance on widely used apps or frameworks, opcache' |
- | (Currently, Opcache only optimizes type checks that are converted to type check opcodes such as '' | + | (Currently, Opcache only optimizes type checks that are converted to type check opcodes such as '' |
===== Discussion ===== | ===== Discussion ===== | ||
==== Possibility of naming conflicts with future vector-like types ==== | ==== Possibility of naming conflicts with future vector-like types ==== | ||
+ | |||
+ | Originally, this was called '' | ||
https:// | https:// | ||
Line 91: | Line 94: | ||
(e.g. only checked during property assignment, passing in arguments, and returning values), then it wouldn' | (e.g. only checked during property assignment, passing in arguments, and returning values), then it wouldn' | ||
- | - is_array_list() or is_array_and_list() or array_is_list() would avoid some of that ambiguity but would be much more verbose. Many existing array methods only accept arrays, making the name '' | + | - '' |
- | - It is very possible that we may end up using the word '' | + | - It is very possible that we may end up using the word '' |
- The name '' | - The name '' | ||
Line 102: | Line 105: | ||
are correct at compile time - because PHP has no bundled type checker, a new type would potentially cause a lot of unintuitive behaviors. | are correct at compile time - because PHP has no bundled type checker, a new type would potentially cause a lot of unintuitive behaviors. | ||
- | ===== Proposed Voting Choices | + | Additionally, |
- | Yes/No, requiring 2/3 majority | + | |
+ | ===== Vote ===== | ||
+ | |||
+ | Voting started on 2021-01-06 and ended 2021-01-20 | ||
+ | |||
+ | This is a Yes/ | ||
+ | |||
+ | <doodle title=" | ||
+ | * Yes | ||
+ | * No | ||
+ | </ | ||
===== References ===== | ===== References ===== | ||
Line 111: | Line 124: | ||
* https:// | * https:// | ||
* https:// | * https:// | ||
+ | * https:// | ||
+ | |||
===== Rejected Features ===== | ===== Rejected Features ===== | ||
+ | |||
+ | ==== Alternate names ==== | ||
+ | |||
+ | '' | ||
+ | |||
+ | '' | ||
==== Alternate implementations ==== | ==== Alternate implementations ==== | ||
- | Making the signature '' | + | The signature '' |
- | Similar to '' | + | and the behavior |
This deliberately only returns true for arrays with sequential keys and a start offset of 0. It returns false for '' | This deliberately only returns true for arrays with sequential keys and a start offset of 0. It returns false for '' | ||
- | This deliberately | + | This deliberately |
+ | |||
+ | ==== Adding flags to is_array() ==== | ||
+ | |||
+ | https:// | ||
+ | < | ||
+ | I actually like the idea of flags added to is_array() for this. | ||
+ | |||
+ | Something like: | ||
+ | |||
+ | <code php> | ||
+ | is_array($value, | ||
+ | </ | ||
+ | |||
+ | I’m not suggesting these names; they’re for illustration only. | ||
+ | </ | ||
+ | |||
+ | I'm strongly opposed to adding any flags to '' | ||
+ | The addition of flags has a small impact on performance for calls that aren't unambiguously qualified (especially if using both), and it makes it harder to see issues like | ||
+ | '' | ||
==== Changes to PHP's type system ==== | ==== Changes to PHP's type system ==== | ||
- | **This RFC does not attempt to change php's type system.** External static analyzers may still benefit from inferring key types from '' | + | **This RFC does not attempt to change php's type system.** External static analyzers may still benefit from inferring key types from '' |
Any attempt to change php's type system would need to deal with references and the global scope - e.g. what would happen if an array was passed to '' | Any attempt to change php's type system would need to deal with references and the global scope - e.g. what would happen if an array was passed to '' | ||
Line 163: | Line 203: | ||
Those are all solvable problems (and I've likely forgotten several), but they would have to be thought through extensively before an implementation could be viable. | Those are all solvable problems (and I've likely forgotten several), but they would have to be thought through extensively before an implementation could be viable. | ||
</ | </ | ||
+ | |||
+ | ===== Changelog ====== | ||
+ | |||
+ | * 0.3: Change name and signature from '' | ||
+ | * 0.2: Rename from '' | ||
+ |
rfc/is_list.1608495724.txt.gz · Last modified: 2020/12/20 20:22 by tandre