Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

request for enhancement with variadics #664

Closed
ghost opened this issue Apr 10, 2018 · 8 comments
Closed

request for enhancement with variadics #664

ghost opened this issue Apr 10, 2018 · 8 comments

Comments

@ghost
Copy link

ghost commented Apr 10, 2018

could psalm infer a closure param type if the input to array_map was taken from a typed variadic ? (i.e. to upgrade the error message from "could not infer type")

<?php
function Foo(DateTime ...$dateTimes) : array {
  return array_map(function ($dateTime) : string {
    return (string) ($dateTime->format('c'));
  }, $dateTimes);
}
@muglug
Copy link
Collaborator

muglug commented Apr 10, 2018

I won't do exactly what you asked, but I can have Psalm produce an error message similar to the one it produces here:

Argument 1 of datetimeArrayMapper expects callable(DateTime):mixed, callable(mixed):string provided

/**
 * @param callable(DateTime):mixed $c
 * @param DateTime[] $arr
 * @return mixed[]
 */
function datetimeArrayMapper(callable $c, array $arr) : array {
    $new_arr = [];
    foreach ($arr as $a) {
        $new_arr[] = $c($a);
    }
    return $new_arr;
}

function foo(DateTime ...$dateTimes) : array {
    return datetimeArrayMapper(
        function ($dateTime) : string {
            return "hello";
        },
        $dateTimes
    );
}

@muglug
Copy link
Collaborator

muglug commented Apr 10, 2018

Actually that's not useful either.

@muglug
Copy link
Collaborator

muglug commented Apr 10, 2018

Related to #204

@ghost
Copy link
Author

ghost commented Apr 11, 2018

I wasn't aware of the callable(T1):T2 syntax- was that part of your discarded resolution, or is it already in psalm?

@weirdan
Copy link
Collaborator

weirdan commented Apr 11, 2018

@WDD-Marv It's in dev-master only, there was no release since it was added AFAIK.

@muglug
Copy link
Collaborator

muglug commented Apr 11, 2018

Yup, hopefully a release tonight with that feature.

The problem with adding an required param type of callable(Foo):mixed when array_map is passed Foo[] in its second arg is that Foo is a lower bound on the callable’s param type. You should be able to pass it callable(FooParent):mixed without issue, but adding that constraint I mentioned above breaks that.

@iluuu1994
Copy link
Contributor

I would like this feature.

https://psalm.dev/r/7790a6ea53

$ints = [1, 2, 3, 4];
$ints2 = array_filter($ints, function ($int) {
  return $int->foo();
});

This example emits a few infos but no errors. Most modern languages allow emitting types in closures when it can be inferred (Swift, C#, Rust, etc.). I only really add them because it helps with static analysis and my IDE.

Psalm could still error when the type cannot be inferred of course.

@muglug muglug closed this as completed in d7ee952 May 7, 2019
@muglug
Copy link
Collaborator

muglug commented May 7, 2019

Solved (in a slightly hacky fashion) using the scaffolding I added yesterday to fix #1600

This now typechecks (and Psalm finds bugs in the array_filter example)

https://psalm.dev/r/dc741dce60

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants