Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save acrylic-origami/368339195e2bb7dc449618468b31eeae to your computer and use it in GitHub Desktop.
Save acrylic-origami/368339195e2bb7dc449618468b31eeae to your computer and use it in GitHub Desktop.
A contravariant variation of #7585 that doesn't allow a violation.
<?hh // strict
<<__ConsistentConstruct>>
abstract class ConWrapper<-T> {
public function __construct() {}
abstract public function act(T $v): void;
}
class ConcreteWrapper extends ConWrapper<OtherBase> {
public function act(OtherBase $v): void {
$v->fn();
}
}
class Base {}
class OtherBase {
public function fn(): void {}
}
function violate(classname<ConWrapper<OtherBase>> $C): void {
(new $C())->act(new Base()); // Invalid argument (Typing[4110])
}
// attempted violation: violate(ConcreteWrapper::class)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment