Req #70016 [Opn]: SplDoublyLinkedList should return ::key() for key()

From: Date: Mon, 19 Apr 2021 12:49:10 +0000
Subject: Req #70016 [Opn]: SplDoublyLinkedList should return ::key() for key()
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-233508@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=70016&edit=1 ID: 70016 Updated by: nikic@php.net Reported by: me at evertpot dot com Summary: SplDoublyLinkedList should return ::key() for key() Status: Open Type: Feature/Change Request Package: SPL related Operating System: Any PHP Version: 5.5.26 -Assigned To: +Assigned To: nikic Block user comment: N Private report: N New Comment: Assigning to self to track deprecation (https://wiki.php.net/rfc/deprecations_php_8_1#key_current_next_prev_reset_on_objects) Previous Comments: ------------------------------------------------------------------------ [2015-08-26 21:23:47] cmb@php.net I still don't think this is a bug, so I'm switching back to feature request. I don't deny that it might be nice to extend key(), current() and next() to work on all Traversables (even though I personally very rarely use these functions at all), several other array functions should also be extended the same way for even more consistency, see also <https://github.com/php/php-src/pull/1385#issuecomment-121211253>. ------------------------------------------------------------------------ [2015-08-18 23:36:13] me at evertpot dot com You were right. I was under the assumption that key() mapped consistently to Iterator::key(), but this is not true at all. In addition, I was also under the assumption that for generators, this syntax: yield $key => $value would also map to key(), but this is not true either. I also found out that neither current() nor next() actually give meaningful results on the result of a generator function. That does seem like a bug, as it works for other iterators. So I'm sorry for the back and forward. I know you guys must get a lot of reports. While I still think that key(), current() and next() should *just* work for any iterator, generator result or array, you were right that at least in the case of key() this was never the case for *most* iterators. So, up to you how you want to treat this. ------------------------------------------------------------------------ [2015-08-18 22:43:28] cmb@php.net > current, next and key work on every Traversable. Albeit not necessarily in a meaningful way, see <https://3v4l.org/4EZrT>. > The signature in the docs is simply *also* a bug, albeit a > documentation bug. Well, I don't think so, because key() internally expects a HashTable as argument[1]. Passing an arbitrary Traversable might cause undesirable overhead (and maybe worse; consider generators). Anyway, as you are insisting that this is a bug, I'm changing back to bug. [1] <https://github.com/php/php-src/blob/php-5.6.12/ext/standard/array.c#L928-L937> ------------------------------------------------------------------------ [2015-08-18 21:39:14] me at evertpot dot com current, next and key work on every Traversable. The signature in the docs is simply *also* a bug, albeit a documentation bug. See here for an alternative example using ArrayAccess: https://3v4l.org/j0ab9 This has been a PHP feature since the introduction of Iterator. ------------------------------------------------------------------------ [2015-08-18 18:44:28] cmb@php.net This is not a bug, see the signature of key(): | mixed key ( array &$array ) ------------------------------------------------------------------------ The remainder of the comments for this report are too long. To view the rest of the comments, please view the bug report online at https://bugs.php.net/bug.php?id=70016 -- Edit this bug report at https://bugs.php.net/bug.php?id=70016&edit=1

« previous php.bugs (#233508) next »