Skip to content

Instantly share code, notes, and snippets.

@acrylic-origami
Last active June 22, 2018 02:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save acrylic-origami/89857fff7980bff31cfbd131f37672a5 to your computer and use it in GitHub Desktop.
Save acrylic-origami/89857fff7980bff31cfbd131f37672a5 to your computer and use it in GitHub Desktop.
Why `this` in a contravariant position in an interface doesn't work out
<?hh // strict
interface Base {
public function foo(this $v): void;
}
final class Derived<T> implements Base {
public function bar(): void {}
public function foo(this $v): void {
$v->bar(); // an `OtherDerived` here would be unwelcome.
}
}
final class OtherDerived implements Base {
public function foo(this $v): void {}
}
function violate(Base $v, Base $x): void {
$v->foo($x);
}
/* HH_FIXME[1002] Try to violate in strict mode */
violate(new Derived(), new OtherDerived());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment