-
Notifications
You must be signed in to change notification settings - Fork 663
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
Feature: use return type providers for inferred return types #8721
Comments
I found these snippets: https://psalm.dev/r/90304e4579<?php
/**
* @param array $a
* @param array $b
* @return array
*/
function foo( $a, $b ) {
$tmp = array_merge( $a, $b );
/** @psalm-trace $tmp */;
return $tmp;
}
$y = array( 'foo' => 'bar' );
$z = array( 'hello' => 'world' );
$first = array_merge( $y, $z );
/** @psalm-trace $first */;
$second = foo( $y, $z );
/** @psalm-trace $second */;
|
A long-time todo of mine, would really help in legacy codebases especially. |
Yes, but not only there, since the |
This would be a separate issue regarding template support for intersection of keyed arrays (#7600 is somewhat related), though it might be tricky to implement properly for usecases other than the simple "enhance" example mentioned in #7600 due to the different semantics of |
I've been planning to implement something really cool to infer literal types: automatic inlining of functions and full literal type inferring for pure native functions (I actually already submitted a PR once for this last feature #5723, might be worth to re-submit a slightly tweaked version...) |
Meanwhile you can do this: https://psalm.dev/r/662034762d |
I found these snippets: https://psalm.dev/r/662034762d<?php
/**
* @template TK1 of array-key
* @template TV1 of mixed
* @template TK2 of array-key
* @template TV2 of mixed
* @param array<TK1,TV1> $a
* @param array<TK2,TV2> $b
* @return array<TK1|TK2,TV1|TV2>
*/
function foo( $a, $b ) {
$tmp = array_merge( $a, $b );
/** @psalm-trace $tmp */;
return $tmp;
}
$y = array( 'foo' => 'bar' );
$z = array( 'hello' => 'world' );
$first = array_merge( $y, $z );
/** @psalm-trace $first */;
$second = foo( $y, $z );
/** @psalm-trace $second */;
|
@weirdan this doesn't work correctly if you have arrays with partially identical keys: https://psalm.dev/r/b1dd74bbf1
is wrong, bc it should be (like $first correctly shows)
|
I found these snippets: https://psalm.dev/r/b1dd74bbf1<?php
/**
* @template TK1 of array-key
* @template TV1 of mixed
* @template TK2 of array-key
* @template TV2 of mixed
* @param array<TK1,TV1> $a
* @param array<TK2,TV2> $b
* @return array<TK1|TK2,TV1|TV2>
*/
function foo( $a, $b ) {
$tmp = array_merge( $a, $b );
/** @psalm-trace $tmp */;
return $tmp;
}
$y = array( 'foo' => 'bar', 'hello' => 'world' );
$z = array( 'hello' => 'bar' );
$first = array_merge( $y, $z );
/** @psalm-trace $first */;
$second = foo( $y, $z );
/** @psalm-trace $second */;
|
The type is not wrong per se, but it's not as tight as it potentially could be. And key-value correspondence is lost too. |
Not really, bc the value can never be |
I found these snippets: https://psalm.dev/r/b1dd74bbf1<?php
/**
* @template TK1 of array-key
* @template TV1 of mixed
* @template TK2 of array-key
* @template TV2 of mixed
* @param array<TK1,TV1> $a
* @param array<TK2,TV2> $b
* @return array<TK1|TK2,TV1|TV2>
*/
function foo( $a, $b ) {
$tmp = array_merge( $a, $b );
/** @psalm-trace $tmp */;
return $tmp;
}
$y = array( 'foo' => 'bar', 'hello' => 'world' );
$z = array( 'hello' => 'bar' );
$first = array_merge( $y, $z );
/** @psalm-trace $first */;
$second = foo( $y, $z );
/** @psalm-trace $second */;
|
https://psalm.dev/r/90304e4579
Ideally psalm would correctly infer that $first and $second are identical
The text was updated successfully, but these errors were encountered: