On Thu, May 16, 2024, at 22:31, Patrik Václavek wrote:
> This feature aims to simplify and standardize the process of verifying that a 
> variable is an instance of a specific class, enhancing code readability and 
> reducing boilerplate code.
> 
> Currently, in PHP, to ensure that a variable is an instance of a specific 
> class, developers need to use the `instanceof` operator and manually throw an 
> exception if the check fails. This results in repetitive boilerplate code 
> scattered throughout the codebase. A new syntax, `(ClassName) $variable`, is 
> proposed to streamline this process by performing an instanceof check and 
> throwing a `TypeError` if the variable is not an instance of the specified 
> class.

I view variables changing types to be an anti-pattern.

Instead if adding type guards, I would rather see this problem solved by having 
typed local variables. This ensures that a variable always holds a specific 
type, avoiding the need of repetitive scattered type guards. Working with 
variables would then be the same as working with class properties. They could 
be typed or untyped.

Also, I see the need for type guards as a symptom of bad composition. Mostly 
methods that are too large would benefit. If a method is decomposed into 
smaller methods the use of parameter and return type checks it's unlikely there 
is need for additional type guards.

Reply via email to