Skip to content

[12.x] Types: EnumeratesValues Sum #55044

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

Merged
merged 4 commits into from
Mar 16, 2025

Conversation

liamduckett
Copy link
Contributor

Hey, this PR adds a more specific typehint to the return value of EnumeratesValues::sum.

$amount = $transactions->sum(fn (Transaction $transaction) => $transaction->amount);

// Currently: mixed
// After PR:  float

After implementing, I discovered that this doesn't work when the return type is literal. For example:

$amount = $transactions->sum(fn (Transaction $transaction) => 5);

// Currently: mixed
// After PR:  5
// Actual:    int

I don't believe this is a big deal for two reasons:

  1. Passing a callable with a literal return type to sum doesn't feel realistic.
  2. This issue already exists in EnumeratesValues::reduce (see below snippet)
$collection->reduce(fn(int $int, Transaction $transaction, int $key) => 5, 3)

// Currently: 5
// Actual:    3|5

Open to other ideas, that would fix my use case without breaking this (albeit seemingly strange) other one!

@taylorotwell taylorotwell merged commit ae347cb into laravel:12.x Mar 16, 2025
41 checks passed
@AJenbo
Copy link
Contributor

AJenbo commented Mar 19, 2025

Nice work, this removed the need for a bunch of workarounds

@liamduckett
Copy link
Contributor Author

Thanks @AJenbo! 😃

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