-
-
Notifications
You must be signed in to change notification settings - Fork 71
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
fix(option): return Option<never>
for Option::none()
#415
Conversation
I'm okay with this change, but i fear it might be a BC break. wdyt @veewee? |
Pull Request Test Coverage Report for Build 5589071290
💛 - Coveralls |
I don't think this is a breaking change because of: If you create an union of However, I do think we also need to add this never return to the regular namespace Psl\Option;
/**
* Create an option with none value.
*
* @return Option<never>
*/
function none(): Option
{
return Option::none();
} |
I found these snippets: https://psalm.dev/r/9715a63a4f<?php
/** @var string|never $x */
$x = 'dd';
/** @psalm-trace $x */
|
It may be a breaking change in some cases. /**
* @return Option<string>
*/
public function test(): Option
{
/** @var Option<string> */
return none();
} I'm not currently using Psalm so I'm unsure if this is a linting error or just a feature to remove "dead" code that does not belong to the static analysis part, but there is a redundant /**
* @return Option<string>
*/
public function test(): Option
{
/** @phpstan-ignore-next-line */
return none();
} In this example, PHPStan would throw an In both cases, I'm not sure if I would consider these BC changes in static analysis, or just regular fixes, but it will change a couple of baselines too. Edit:
I think it would not be necessary, at least in PHPStan. Maybe Psalm would require it, idk |
In psalm, you have to define it because of this: If you don't specify the template, it will take I've reported an issue before in psalm regarding this 'optional' generic: You might be able to use
It would rather be for consistency than for fixing a SAT complaint. If |
I found these snippets: https://psalm.dev/r/068ab0cbf1<?php
/** @template T */
class Option {}
/** @return Option<never> */
function never(): Option {
/** @var Option<never> */
return new Option();
}
/** @return Option<never> */
function mixed(): Option {
return new Option();
}
/** @return Option<string> */
function stringy(): Option {
return new Option();
}
https://psalm.dev/r/c6ab6659f3<?php
/** @template T */
class Option {}
/** @return Option<never> */
function none(): Option {
// Note : you won't need this var when returning Option::none()
/** @var Option<never> */
return new Option();
}
/**
* @template T
* @param T $x
* @return Option<T>
*/
function some($x): Option {
return new Option();
}
/** @return Option<literal-string> */
function maybe() {
return rand(0,1) ? some('hello') : none();
}
|
You are right @veewee, the I'm not scanning your project with PHPStan as I guess you are not trying to be PHPStan compliant. If you think it can be interesting to pass both static analysis, I could start a draft if you are ok with that 😊 |
@devnix would welcome that, there's no harm in having it here too. |
Option<never>
for Option::none()
Option<never>
for Option::none()
This change would make PHPStan generics work with these kinds of examples:
Which right now throws an error like