Skip to content
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

feat: improve return types for Collection::first, last, get and pull when default value is given. #1010

Merged

Conversation

bastien-phi
Copy link
Contributor

@bastien-phi bastien-phi commented Nov 5, 2021

  • Added or updated tests
  • Documented user facing changes
  • Updated CHANGELOG.md

Changes

This PR improves return type resolution for Collection::first, last, get and pull when default value is given.

Today,

User::all()->first(default: new User);

is typed as User|null, even if it actually never returns null.

With this PR, it will be typed as User.

Strange calls are also supported

User::all()->first(default: 42); // User|int

(I really don't know why someone would write this but why not ?)

This PR does not add support for callable as default

User::all()->first(default: fn() => new User);

My guess is it will be a little bit more complicated than just a stub update.

Breaking changes

I hope there won't be any

@szepeviktor
Copy link
Collaborator

Thank you.

CHANGELOG.md Outdated Show resolved Hide resolved
Copy link
Collaborator

@canvural canvural left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi,

Thank you. It looks good. Just left two tiny comments.

stubs/Collection.stub Outdated Show resolved Hide resolved
stubs/Collection.stub Outdated Show resolved Hide resolved
@bastien-phi bastien-phi force-pushed the improve_default_values_in_collection branch from 32fa4da to 99a81e6 Compare November 8, 2021 15:23
Copy link
Collaborator

@canvural canvural left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you can rebase and fix the conflicts we can merge! 👍🏽

@bastien-phi bastien-phi force-pushed the improve_default_values_in_collection branch from 99a81e6 to cd36beb Compare November 9, 2021 09:35
@bastien-phi
Copy link
Contributor Author

Should be good !

CHANGELOG.md Outdated Show resolved Hide resolved
@canvural canvural changed the title Improve return types for Collection::first, last, get and pull when default value is given. feat: improve return types for Collection::first, last, get and pull when default value is given. Nov 9, 2021
@canvural canvural merged commit 77a85d9 into larastan:master Nov 9, 2021
@canvural
Copy link
Collaborator

canvural commented Nov 9, 2021

Thank you!

@bastien-phi bastien-phi deleted the improve_default_values_in_collection branch November 9, 2021 09:49
@bastien-phi
Copy link
Contributor Author

Glad to contribute !

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.

None yet

3 participants