Hack: Allow providing additional information to the typechecker
I'm writing a lot of tests in Hack at the moment, using my phpunit-hhi package to tell Hack about PHPUnit.
It would be useful to be able to get Hack to know about the semantics of some of the methods, rather than doing the assert, then adding in an invariant directly afterwards.
For example, I'm doing:
$this->assertNotNull($foo);
invariant($foo !== null, 'For Hack');
Would be nice if there was someway to annotate the methods so that Hack knew that if assertNotNull doesn't throw, then what's passed in is not null. Likewise for assertNull and assertInstanceOf.
An alternative is that phpunit is popular enough we might want to special-case this.
As a side note, should assertInstanceOf take a classname
It should now that 3.9 is out. Will be doing that as part of updating the HHI to PHPUnit 4.7.
Hack explicitly does not want any function calls to refine. Even \is_string() and friends lost that ability in 3.30. If something like you are requesting were to be introduced, it would most likely only apply for free functions. If methods were able to refine something, you'd need to know the type of the receiver (the object the method is being called on). Discovering more types would then force Hack to redo control flow analysis, which could then in turn change the type of a receiver. This would result in very difficult to debug type errors for end users, where failure to refine such a special object would result in a type error a mile away.
I agree that the repro you gave was a pointed example. The modern day equivalent would be:
use function Facebook\FBExpect\expect;
// ...
expect($some_value)->toNotBeNull();
toNotBeNull sort-of addresses this specific case: it returns T where the input is ?T