Skip to content

never union type #20728

@kkmuffme

Description

@kkmuffme

Description

function foo(): bool|never {
    if (rand(0,1)) {
        echo "false";
        return false;
    }
    
    echo "exit";
    exit;
}

foo();

https://www.php.net/manual/en/language.types.never.php

never is a return-only type indicating the function does not terminate
it cannot be part of a union type declaration

What if a function only terminates conditionally like in the example above? Currently using "bool" type hint works above, however that loses relevant information, which currently can only be conveyed with a phpdoc comment.

The docs state:

Therefore, it cannot be part of a union type

but there is nothing "therefore" about it and no reason why it cannot be part of a union type
(except PHP itself "enforcing" no union types of overlapping built-in types - but if we look at the int/float behavior of allowing int, this isn't strictly true anyway, and not true at all when taking non-built in types into account, e.g. with child/parent classes)

This has been working in static analysis (psalm, phpstorm) since ages too and I think is somthing that should be natively supported too.
(I'm the one who implemented this in psalm back then)

In turns of code runtime this requires no difference at all, since behavior does not change in any case.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions