On Sun, Jul 2, 2023 at 7:08 PM Ben Ramsey <b...@benramsey.com> wrote:
>
> On Jul 2, 2023, at 19:11, Levi Morrison <morrison.l...@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;
```

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: https://www.php.net/unsub.php

Reply via email to