Skip to content

[12.x] Types: EnumeratesValues Sum#55044

Merged
taylorotwell merged 4 commits into
laravel:12.xfrom
liamduckett:types/enumerates-values-sum
Mar 16, 2025
Merged

[12.x] Types: EnumeratesValues Sum#55044
taylorotwell merged 4 commits into
laravel:12.xfrom
liamduckett:types/enumerates-values-sum

Conversation

@liamduckett

Copy link
Copy Markdown
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!

@AJenbo

AJenbo commented Mar 19, 2025

Copy link
Copy Markdown
Contributor

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

@liamduckett

Copy link
Copy Markdown
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