Skip to content

Improve Option::select() return type - #76

Closed
axlon wants to merge 1 commit into
schmittjoh:masterfrom
axlon:select-return-type
Closed

Improve Option::select() return type#76
axlon wants to merge 1 commit into
schmittjoh:masterfrom
axlon:select-return-type

Conversation

@axlon

@axlon axlon commented Aug 11, 2024

Copy link
Copy Markdown
Contributor

This change adds a minor improvement to the return type of Option::select() for static analysis.

/** @var \PhpOption\Option<1|2|3> $option */
-PHPStan\dumpType($option->select(2)); // PhpOption\Option<1|2|3>
+PHPStan\dumpType($option->select(2)); // PhpOption\Option<2>

@GrahamCampbell

Copy link
Copy Markdown
Collaborator

Thanks for the PR, but I wasn't able to reproduce the improvement: on current PHPStan (1.12 and 2.x alike) and Psalm, select(2) on Option<1|2|3> still resolves to Option<1|2|3>, because the inferred template type is generalized back to its bound, so this change has no observable effect. If analyzers ever did start honoring the bound, the narrowing would interact badly with Option's invariant T and produce new errors wherever a select() result flows back into an Option<T> position. Closing, since as it stands this adds a latent behaviour change without a demonstrable benefit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants