Skip to content

How are constructor types inferred? #4

Open
@nikic

Description

@nikic

The description in https://github.com/DaveLiddament/php-generics-standard#constructor is a bit unclear to me. If you are passing a literal integer value, it's obvious that T = int, but what if the value is dynamic?

function test(?int $foo) {
    $valueHolder = new ValueHolder($foo);
}

In this case, $foo can be int or null at runtime. However, I would assume that the actual inferred type is T = ?int. Is that correct?

If it is, then what about this variant?

function test(?int $foo) {
    if (null !== $foo) {
        $valueHolder = new ValueHolder($foo);
    }
}

Here, $foo is known to be int, but only through flow-sensitive type analysis. Would T = int here?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions