hhvm icon indicating copy to clipboard operation
hhvm copied to clipboard

Hack: Allow extending shapes

Open simonwelsh opened this issue 9 years ago • 10 comments

We're starting to move towards using shapes as option arguments and are having to duplicate most of the declaration between subclasses, which leads to problems when changing the base class.

Contrived example:

<?hh // strict

type AOptions = shape(
  'a' => int,
  'b' => ?string,
);

type BOptions = shape(
  'a' => int,
  'b' => ?string,
  'c' => (function():void),
); 

class A {
  public function __construct(protected AOptions $options) {}
}

class B extends A {
  protected (function():void) $c;

  public function __construct(BOptions $options) {
    $this->c = $options['c'];
    parent::__construct($options);
  }
}

Being able to have BOptions extend AOptions would reduce the copy pasta and chance of errors. Something like:

type BOptions = shape(
  'c' => (function():void),
) extends AOptions;

simonwelsh avatar Jun 05 '16 00:06 simonwelsh