Open thomasvargiu opened 3 years ago
I found these snippets:
Looking at this example: https://psalm.dev/r/b6c1411ec4
I think it should be possible.
foo('foo')
should return callable(string(foo)): B:fn-foo
B-foo
should continue to be a templated type because is recognized as a deferred type, because it's a result of a parameter (the callable)callable(string(foo)): B:fn-foo
and infer return type from the caller@weirdan what do you think?
I found these snippets:
That would mean foo
should return a generic callable (something like callable<T of mixed>(callable(string(foo)): T): T
) and to my knowledge Psalm does not support generic callables at the moment.
Technically this does typecheck and run (https://psalm.dev/r/b618884527, https://3v4l.org/EHf6N) when rewritten using a class, so the question is all about the docblock syntax.
I found these snippets:
In your example you are wrapping almost the same logic in a invokable class, but with the same behaviour. But it's impossible with already existing librararies, and excessive to creare a class for a simple lambda function, and I think it's something that will be use more in the next years starting from PHP 7.4 with arrow functions.
I was trying to do it writing a plugin (FunctionReturnTypeProviderInterface
) for a pipe()
function (composition of one or more callables, pipe(f1, f2, f3)($a)
), returning a chain of callables keeping the "generics, and then evaluating calls with a
AfterStatementAnalysisInterface`, revaluating variables in context. Obviously I was just trying and playing with it, but with more knolodgment I think it would be possible to make a plugin for it (at least for a specific function).
Now, a function like that will use empty
and not even mixed
, so it's a little bit complicated to use it writing the right return type.
In your example you are wrapping almost the same logic in a invokable class, but with the same behaviour. But it's impossible with already existing librararies
Indeed. The point of this exercise was to see how it maps to concepts already used in Psalm and to check if it's sound. I'm not suggesting you to actually rewrite anything.
Oh, maybe as the first thing, should psalm support templates in closures? Or I'm doing something wrong here? https://psalm.dev/r/79c407cbf4
That's why my experimental plugin wasn't working :)
I found these snippets:
Probably related to #4550
Consider the following two examples. I think it should be possible to keep track of template references and infer the right type of the return type.
https://psalm.dev/r/6e44e1eccf https://psalm.dev/r/4efcc7e309
For example, in the above code, as the interal psalm code does, function
foo()
should return acallable(B:fn-foo as mixed): B:fn-foo as mixed
, so that calling this callable we know that the return type is the first argument.Same in the following code:
Here, nested callables should keep track of the template.
Without this improvement, it's almost impossible to work with nested callables, specially developing or documenting a functional programming library.
I think it should be possible, right?