Re: [VOTE] Interface Default Methods
| From: | Levi Morrison | Date: | Mon, 03 Jul 2023 02:43:27 +0000 |
| Subject: | Re: [VOTE] Interface Default Methods | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-120728@lists.php.net to get a copy of this message | ||
On Sun, Jul 2, 2023 at 7:08 PM Ben Ramsey <ben@benramsey.com> wrote:
>
> On Jul 2, 2023, at 19:11, Levi Morrison <morrison.levi@gmail.com> wrote:
>
> Chatter on the [Interface Default Methods RFC][1] has been quiet for
> the past 6 days, and the feature freeze deadline is fast approaching
> for PHP 8.3, so I'm moving this to vote. It'll be open for two weeks
> as usual.
>
> Thanks to everyone who discussed weaknesses in the RFC during the
> discussion phase.
>
> [1]: https://wiki.php.net/rfc/interface-default-methods
>
>
>
> I’m probably going to vote “yes” on this, but since the property accessors RFC won’t be
> ready for 8.3,[1] and that RFC covers use of properties in interfaces, how useful will interface
> default methods be without interface properties?
Plenty useful, I think. Interfaces deal with abstractions, so there
are often things you can do without having state. Java's interfaces
don't allow properties, for instance, and they still have this
feature.
As an example, this poor-man Sequence interface works, because the
behavior and state for
next is provided by the implementer:
```php
<?php
interface Sequence {
// return null to indicate the end of sequence
function next();
function fold($initial, callable $f) {
$reduction = $initial;
while (($next = $this->next()) !== null) {
$reduction = $f($reduction, $next);
}
return $reduction;
}
function reduce(callable $f) {
$next = $this->next();
return $next !== null
? $this->fold($next, $f)
: null;
}
function into_array_list(): array {
$array = [];
while (($next = $this->next()) !== null) {
$array[] = $next;
}
return $array;
}
}
final class IteratorSequence
implements Sequence
{
function __construct(private Iterator $iter) {
$this->iter->rewind();
}
function next() {
$iter = $this->iter;
if ($iter->valid()) {
$next = $iter->current();
$iter->next();
return $next;
}
return null;
}
}
function xrange($begin, $end) {
$i = $begin;
while ($i < $end) {
yield $i++;
}
}
$seq = new IteratorSequence(xrange(1, 10));
var_export($seq->into_array_list());
echo PHP_EOL;
```