facebook / facebook/hhvm

Generics that resolve to `this` fail the type checker

Open
#4,529 4 comments 0 reactions 0 assignees View on GitHub
hack
Dominant language
C++
Stars
18.7k
Forks
3.1k
Avg merge
1h 47m
Merged PRs (30d)
2

Description

I have this caching method that uses a closure to do the caching. The current class instance is passed as the 1st argument to the closure. I'm trying to type check this through generics and am encountering this error.

```
/vagrant/src/Titon/Common/Cacheable.hh:56:44,48: Invalid argument (Typing[4110])
/vagrant/src/Titon/Common/Cacheable.hh:49:52,52: This is a value of generic type T
/vagrant/src/Titon/Common/Cacheable.hh:56:44,48: It is incompatible with an object of type Titon\Common\Cacheable
/vagrant/src/Titon/Common/Cacheable.hh:56:44,48: Considering the constraint on the type 'this'
```

And the method in question. https://github.com/titon/framework/blob/master/src/Titon/Common/Cacheable.hh#L49

If I remove the generics, then I get this error. (It also causes all classes that implement the method to throw errors).

```
/vagrant/src/Titon/Common/Cacheable.hh:56:18,49: Too many arguments (Typing[4105])
/vagrant/src/Titon/Common/Cacheable.hh:49:39,57: Definition is here
```

If I use a variadic callback type annotation `(function(...): mixed)`, then I get this error (in all the implementing classes).

```
/vagrant/src/Titon/View/EngineView.hh:57:73,80: Invalid argument (Typing[4039])
/vagrant/src/Titon/Common/Cacheable.hh:49:39,60: Number of arguments doesn't match
/vagrant/src/Titon/View/EngineView.hh:57:73,80: Because of this definition
```

So... is there a way to do this? Or is this a bug?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.