-
Notifications
You must be signed in to change notification settings - Fork 660
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
Contextual type inference for high order function arg #7417
Contextual type inference for high order function arg #7417
Conversation
Can you add a snippet in test to show the easiest possible code that is solved by this PR? Also could you add some comments in the code describing what you're doing. It will help for the review and also makes easier to read this in the future |
Done. If there is anything else that needs to be added, please let me know) |
Sorry, I should have warned before but could you target this on master? We'll start avoiding to land big new features on 4.x to prevent maintenance on old versions once Psalm 5 is out |
OMG !!! I don't have much time these days, but I'm planning to review that and the work of @veewee ! |
I think @Ocramius, @Crell and @AlexandruGG might be interested as well ! |
Oh my. I don't know the Psalm codebase well enough, but the generic pipe function already exists: https://github.com/Crell/fp Includes a whole bunch of related utilities as well. It's currently very well-audited by PHPStan, but I haven't tried Psalm on it. (PRs welcome for that.) |
Hello @klimick Nice work on this PR! Will check out the details soon. Some context from my side: I am also trying to build a variadic pipe plugin as well and currently ended up with this:
It works quite well, but only works for callable arguments. Now I am trying to use the NodeTypeProvider in order to get the best type information directly from psalm when calculating the pipe function's attributes type. This is currently not possible yet, since the types of the arguments are not known yet during See:
I guess you probably also need the type information from the provided variadic pipeline stages to build your plugin. This way, you could do something like this inside the code from the pipe plugin mentioned above: private static function parseStages(StatementsSource $source, array $args): array
{
$stages = [];
$nodeTypeProvider = $source->getNodeTypeProvider();
foreach ($args as $arg) {
$stage = $arg->value;
$stageType = $nodeTypeProvider->getType($stage);
$stageClosureTypes = $stageType ? $stageType->getClosureTypes() : []; And base the calculations based on the first Care to take a look at it? :) |
Since php doesn't have extension method functionality like C# then pipe function can be a good solution to the situation:
Before we can write generic pipe function we should support inference for partially applied closures (
fn($a) => fn($b) => $a + $b
)This PR tries to implement inference of this kind.
P.S. I tried to do it through the plugin system. It didn't work out.