Skip to content

Ensure templated closures' return types factor into type resolution - #189

Open
mszabo-wikia wants to merge 1 commit into
mainfrom
reduce-overnarrow
Open

Ensure templated closures' return types factor into type resolution#189
mszabo-wikia wants to merge 1 commit into
mainfrom
reduce-overnarrow

Conversation

@mszabo-wikia

Copy link
Copy Markdown
Collaborator

Currently type resolution for templated closures where a template type is shared by the closure's return type and a different parameter, e.g. reduce<Tv, Ta>(Traversable<Tv>, (function(Ta, Tv): Ta), Ta $initial): Ta is narrowed down only to the type of the parameter. This is incorrect if the closure may produce a wider range of return types, e.g.:

function test(vec<int> $items): Base {
    return reduce(
        $items,
        ($accum, $item) ==> {
            if ($item > 0) {
                return new NoGet();          // accumulator can become NoGet...
            }
            return new HasGet($accum->get()); // ...so $accum is not provably HasGet
        },
        new HasGet("init"),
    );
}

So do a second closure analysis if the inferred param type widens.

Currently type resolution for templated closures where a template type
is shared by the closure's return type and a different parameter,
e.g. `reduce<Tv, Ta>(Traversable<Tv>, (function(Ta, Tv): Ta), Ta $initial): Ta`
is narrowed down only to the type of the parameter.
This is incorrect if the closure may produce a wider range of return types, e.g.:

```hack
function test(vec<int> $items): Base {
    return reduce(
        $items,
        ($accum, $item) ==> {
            if ($item > 0) {
                return new NoGet();          // accumulator can become NoGet...
            }
            return new HasGet($accum->get()); // ...so $accum is not provably HasGet
        },
        new HasGet("init"),
    );
}
```

So do a second closure analysis if the inferred param type widens.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant