Feature request: object-protected and object-private access modifier
- Dominant language
- C++
- Stars
- 18.7k
- Forks
- 3.1k
- Avg merge
- 1h 47m
- Merged PRs (30d)
- 2
Description
### 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:
```php
{ 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 {
public function __construct(
object protected Tracker $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](https://github.com/facebook/hhvm/issues/7216#issuecomment-235726828). Any guesses as to when if ever?
Contributor guide
Assessment
This issue has not been assessed yet.