-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Shapes::keyExists should refine optional shape fields #8012
Comments
The PHP error control operator is not part of hack, and we don't plan on adding it. See https://docs.hhvm.com/hack/typechecker/special for ways to suppress this. |
Seems like a reasonable request, I'll see what we can do. |
We plan on making changes to Hack that will better define when and how type refinement will be performed. We hadn't considered this case, but we will take this use case in mind for the change. I rather focus our attention on the future, especially since we hope to get the feature out within the next 3-6 months. If there was a pull request that made the change I would consider merging it, but not something anyone on the team will work on at the moment. |
I think this issue can be closed with the release of 3.27:
|
For some reason, this doesn't work with invariant. Is this expected? <?hh //strict
function myfunc(shape(?'optional' => string) $var): void {
\invariant(Shapes::keyExists($var, 'optional'), '');
// Invalid index operation: 'optional' is marked as an optional shape field....
print $var['optional'];
}
function myfunc2(shape(?'optional' => string) $var): void {
if (!Shapes::keyExists($var, 'optional')) throw new Exception();
// OK
print $var['optional'];
} hh_client
hhvm --version
|
This might be asking too much of the type checker but it would be nice if this worked on more than 1 level. <?hh //strict
function myfunc(shape(?'optional' => shape(?'optional' => string)) $var): void {
if (!Shapes::keyExists($var, 'optional')) throw new Exception();
if (!Shapes::keyExists($var['optional'], 'optional')) throw new Exception();
// Invalid index operation: 'optional' is marked as an optional shape field....
print $var['optional']['optional'];
} |
HHVM Version
3.22.0 with enable_experimental_tc_features = optional_shape_field
Standalone code, or other way to reproduce the problem
Expected result
Actual result
As it is, dealing with optional fields would require at least 1 Shapes::keyExists and 1 Shapes::idx. This is really really verbose.
Even better solution
Allow the php error control operator (@) to suppress type errors in addition to runtime errors. This has the additional advantage of allowing me to null coalesce optional fields.
The text was updated successfully, but these errors were encountered: