> > What's the advantage of a language construct over the following? > > ```php > /** > * @template T of object > * @psalm-assert T $value > * @param class-string<T> $type > */ > function as(mixed $value, string $type): mixed > { > if (! $value instanceof $type) { throw > SomeKindOfException::forMismatchingRequirements($value, $type); } > > return $value; > } > > echo as(myExpression(), MyType::class)->methodOfMyType(); > ``` > > A static analysis tool supporting something in docblocks should not preclude those things being added in syntax IMO.
Hack's `as` operator is very neat, and it'd be trivial for existing SA tools to support the same in PHP. Here's a demo of `as` in Hack getting flagged by a SA tool: https://hakana.dev/#XQAAgAA2AAAAAAAAAAAzHUn_qWH7EwabJzyN0tdfxv3ug6f7oZ-qScnamcl1qjUZCPmuKA3tD-KFr1f0ZPcrAXt_D1L___KsQAA%3D This would also benefit from a `nonnull` subtype of `mixed` which could be used as a null refinement.