Req #77518 [Csd]: SeekableIterator::seek() should accept 'int' typehint as documented
| From: | nikic@php.net | Date: | Fri, 25 Jan 2019 10:15:32 +0000 |
| Subject: | Req #77518 [Csd]: SeekableIterator::seek() should accept 'int' typehint as documented | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-219194@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=77518&edit=1
ID: 77518
Updated by: nikic@php.net
Reported by: claude dot pache at gmail dot com
Summary: SeekableIterator::seek() should accept 'int'
typehint as documented
Status: Closed
Type: Feature/Change Request
Package: SPL related
PHP Version: 7.3.1
-Assigned To:
+Assigned To: nikic
Block user comment: N
Private report: N
New Comment:
Adding the int type *should* indeed be backwards compatible nowadays. I've added it on master
(7.4) only, since I don't trust that "should" enough to change this on stable
branches.
Previous Comments:
------------------------------------------------------------------------
[2019-01-25 10:13:14] nikic@php.net
Automatic comment on behalf of nikita.ppv@gmail.com
Revision: http://git.php.net/?p=php-src.git;a=commit;h=909d0a3d786118e0b1105288f6b88231e6697815
Log: Fixed bug #77518
------------------------------------------------------------------------
[2019-01-24 20:07:09] claude dot pache at gmail dot com
> unless I am mistaken, the rfc you link has been passed allowing for type hints to be _widened_,
> that is, that declaring an untyped variable could pass a typed variable, whereas what you are doing
> is the inverse.
As a picture is worth a thousand words, here is an illustration:
Current situation
-----------------
interface SeekableIterator extends Iterator {
function seek($position);
}
class LegacyFoo implements SeekableIterator {
// (implementation of Iterator elided)
function seek($position) { } // ok
}
class FutureFoo implements SeekableIterator {
// (implementation of Iterator elided)
function seek(int $position) { } // Error
}
Proposed change
---------------
interface SeekableIterator extends Iterator {
function seek(int $position);
}
class LegacyFoo implements SeekableIterator {
// (implementation of Iterator elided)
function seek($position) { } // ok (since RFC implemented)
}
class FutureFoo implements SeekableIterator {
// (implementation of Iterator elided)
function seek(int $position) { } // ok
}
------------------------------------------------------------------------
[2019-01-24 19:49:50] fractalesque at gmail dot com
Hello,
unless I am mistaken, the rfc you link has been passed allowing for type hints to be _widened_, that
is, that declaring an untyped variable could pass a typed variable, whereas what you are doing is
the inverse.
That is, if the interface was typed with int, you could implement it without the int, but not add
the int where the interface declares untyped.
------------------------------------------------------------------------
[2019-01-24 19:11:01] claude dot pache at gmail dot com
Description:
------------
This is related to Bug #71051, but since
https://wiki.php.net/rfc/parameter-no-type-variance
has been implemented, it should be fixable in PHP7.2+ without breaking legacy code.
Per documentation, the signature of SeekableIterator::seek() is:
abstract public SeekableIterator::seek ( int $position ) : void
However, when implementing the interface, providing the typehint âintâ triggers a compile
error.
Test script:
---------------
class Foo implements SeekableIterator {
function current() { }
function key() { }
function next() { }
function rewind() { }
function valid() { }
function seek(int $position) { }
}
Expected result:
----------------
No error.
Actual result:
--------------
Fatal error: Declaration of Foo::seek(int $position) must be compatible with
SeekableIterator::seek($position)
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=77518&edit=1