-
Notifications
You must be signed in to change notification settings - Fork 667
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
Better support for static<T>
#5383
Comments
I found these snippets: https://psalm.dev/r/b9d50b9872<?php
/** @template T */
abstract class C1 {
/** @param T $value */
final public function __construct(private $value) {}
/**
* @template TFrom
* @param TFrom $value
* @return static<TFrom>
*/
public static function from($value): static {
return new static($value);
}
}
/** @extends C1<string> */
class C2 extends C1 {}
C2::from(false);
|
No idea how to solve this offhand. |
If static method had access to containing class type parameters we could probably require |
Yeah, thinking about it more we probably need to prohibit |
Hmm, I think using
This would be cool, then I would write it like this: https://psalm.dev/r/f42ebc781f But I think I understand why template variables are coupled to instances only. |
I found these snippets: https://psalm.dev/r/f42ebc781f<?php
/** @template T */
abstract class C1 {
/** @param T $value */
public function __construct(private $value) {}
/**
* @param T $value
* @return static<T>
*/
public static function from($value): static {
return new static($value);
}
}
/** @extends C1<string> */
class C2 extends C1 {}
C2::from(false);
|
Yeah, people currently do it, but it's still unsound (as you demonstrate) There's a fundamental difference between The alternative would be to force |
I found these snippets: https://psalm.dev/r/f4989147d8<?php
/** @template T */
abstract class C1 {
/** @param T $value */
final public function __construct(private $value) {}
/**
* @template TFrom
* @param TFrom $value
* @return static<TFrom>
*/
public static function from($value): static {
return new static($value);
}
}
/** @extends C1<string> */
final class C2 extends C1 {
/**
* @param mixed $value
* @return C2
*/
public static function from($value): self {
return new self((string) $value);
}
}
C2::from(false); // now safe
|
I don't think it's too burdensome to tell the maintainer of |
https://psalm.dev/r/b9d50b9872
It would be nice if psalm could fail here, because
static<bool>
does not matchC2 extends C1<string>
.(But I don't know how difficult that would be to implement.)
The text was updated successfully, but these errors were encountered: