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

Template of callable arguments #11104

Open
mathroc opened this issue Sep 26, 2024 · 1 comment
Open

Template of callable arguments #11104

mathroc opened this issue Sep 26, 2024 · 1 comment

Comments

@mathroc
Copy link
Contributor

mathroc commented Sep 26, 2024

Feature request

When wrapping callable, we might want to describe that the callable we return will take the same arguments that the one we got as an argument. (see phpstan/phpstan#6813 for a short discussion on the subject)

Here is an exemple of what the syntax could look like:

https://psalm.dev/r/4e9c0e1d18

I like that it feels right to "expand" the array of types with callable(...A), but I know it might be confusing given callable(A...) as a different meaning


some other use cases for this feature:

/**
 * @template A1
 * @template Arest of array<mixed>
 * @template R
 * @param callable(A1, ...Arest): R
 * @return callable(...Arest): R
 */
function curry(callable $f, mixed $arg1): callable {
  return static fn (...$args) => $f($arg1, ...$args);
}

/**
 * @template A of array<mixed>
 * @template Rf
 * @template Rg
 * @param callable(...A): Rf $f
 * @param callable(Rf): Rg $g
 * @return callable(...A): Rg
function combine(callable $f, callable $g): callable {
   return static fn (...$args) => $g($f(...$args));
}

copied from phpstan/phpstan#6873

Copy link

I found these snippets:

https://psalm.dev/r/4e9c0e1d18
<?php declare(strict_types = 1);

/**
 * @template A of array<mixed>
 * @param \Closure(...A): int $f
 * @return \Closure(...A): string
 */
function castToStringWrapper(\Closure $f): \Closure {
	return static fn (...$args): mixed => (string) $f(...$args);
}

$myRandomStringInt = castToStringWrapper(random_int(...));
Psalm output (using commit 16b24bd):

ERROR: InvalidArgument - 12:42 - Argument 1 of castToStringWrapper expects Closure(array<array-key, mixed>...):int, but pure-Closure(int, int):int provided

INFO: UnusedVariable - 12:1 - $myRandomStringInt is never referenced or the value is not used

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

No branches or pull requests

1 participant