xp-framework / compiler

Compiles future PHP to today's PHP.
19 stars 0 forks source link

Interface Default Methods #150

Closed thekid closed 1 year ago

thekid commented 1 year ago

Can we support the features shown in https://wiki.php.net/rfc/interface-default-methods?

interface Value {
  public function toString();
  public function compareTo($value) {
    return $this <=> $value;
  }
}

class Listing implements Value {
  public function toString() {
    // Needs to be implemented
  }
  public function compareTo($value) {
    // Can be implemented
  }
}

Test cases can be found here: https://github.com/php/php-src/compare/master...morrisonlevi:php-src:interface-default-methods

thekid commented 1 year ago

Could be implemented by a) emitting this as a trait method, b) including this trait in the class:

interface Value {
  public function toString();
  public function compareTo($value);
}

trait __Value_Defaults {
  public function compareTo($value) {
    return $this <=> $value;
  }
}

class Listing implements Value {
  use __Value_Defaults;

  public function toString() {
    // Needs to be implemented
  }
  public function compareTo($value) {
    // Can be implemented
  }
}

...and c) changing the reflection API to suppress "magic" classes starting with __.

thekid commented 1 year ago

There's now a PR for this @ https://github.com/php/php-src/pull/11467

thekid commented 1 year ago

Discussion on PHP internals mailing list @ https://externals.io/message/120582

thekid commented 1 year ago

RFC has been declined