hhvm
hhvm copied to clipboard
Feature request: object-protected and object-private access modifier
HHVM Version
$ hhvm --version
HipHop VM 3.16.0-dev (rel)
Standalone code, or other way to reproduce the problem
For the moment, private parameters and methods can use variant parameters in any position. private
is also the only access modifier that allows these otherwise illegal definitions, but as pointed out in #7216 , it isn't actually typesafe. Instead, Scala's "object-private" and "object-protected" access modifiers would make such declarations sound. Object-private is purely stricter than the current scheme, so the advantage is solely for type safety.
However, object-protected brings something new and powerful, especially with properties. I've found it cumbersome, and sometimes impossible, to cleanly extend classes with variant parameters in properties, because there is no way to interact with them in the subclass in the opposite direction of their variance. For example, #7573 would actually ideally be solved by making ImmutableWrapper::collection
object-protected.
The same is true of generic methods. object protected
as a method access modifier would allow subclasses to override parent definitions of reused internal methods to refine the functionality.
An admittedly artificial but still demonstrative culminating example:
<?hh // strict
interface Tracker<T> { public function track(T $v): void; }
class CovWrapper<+T> {
public function __construct(
object protected T $v
) {
$this->set($v);
}
object protected function set(T $v): void {
$this->v = $v;
}
}
class TrackedWrapper<+T> extends CovWrapper<T> {
public function __construct(
object protected Tracker<T> $tracker,
T $v
) {
parent::__construct($v);
}
<<__Override>>
object protected function set(T $v): void {
$this->tracker->track($v);
parent::set($v);
}
}
andrewjkennedy mentioned that this might come to Hack eventually. Any guesses as to when if ever?
A subset of this feature request is tracked as hhvm/hhast#549.