On 10 Mar 2025, at 20:07, Daniel Scherzer <[email protected]> wrote:
> Hi internals,
>
> I'd like to start discussion on a new RFC about allowing never
for parameter
> types when declaring a method.
>
> * RFC: https://wiki.php.net/rfc/never-parameters-v2
> * Implementation: https://github.com/php/php-src/pull/18016
>
> -Daniel
Hi Daniel,
To begin, I'm all for a never
bottom type, and I use it regularly when I need to
write TypeScript.
However, I think the example offered in this RFC – while a valid one, for now – isn't the
most appropriate use case for a never
type. A bottom type is more often used when
defining conditional types, or to assert that the default for a switch/match expression should never
be reached without raising an error that the switch/match cases are not exhaustive.
As such, a never
type provides a lot of value for static analysis tools and IDEs, and
as a matter of fact both PHPStan[1] and PhpStorm[2] provide ways to declare a bottom type.
Furthermore, when conditional return types and/or generics are finally implemented in PHP, never
will become even more valuable if not essential.
But, when generics eventually do make their way to PHP, it also means the provided example no longer
applies, because you would just be able to use generic types:
```php
interface BackedEnum<T> extends UnitEnum {
public T $value;
public static function from(T $value): static;
public static function tryFrom(T $value): ?static;
}
```
So I think it may help sell your RFC if you provide some additional context illustrating the use of
the never
type in generics and static analysis. Otherwise I fear that some people may
interpret never
as a stopgap solution until we get generics.
Alwin
[1] https://phpstan.org/writing-php-code/phpdoc-types#bottom-type
[2] https://github.com/JetBrains/phpstorm-attributes/blob/master/README.md#noreturn