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

Enumerable#sum(&) fails with certain block arguments #15317

Open
rvprasad opened this issue Jan 1, 2025 · 0 comments
Open

Enumerable#sum(&) fails with certain block arguments #15317

rvprasad opened this issue Jan 1, 2025 · 0 comments
Labels
kind:bug A bug in the code. Does not apply to documentation, specs, etc. status:discussion topic:stdlib:collection

Comments

@rvprasad
Copy link

rvprasad commented Jan 1, 2025

Bug Report

Enumerable.sum(&) should succeed as long as the block argument transforms the elements into values into types that supports addition. This pattern would be common when summing heterogeneous enumerables such as arrays and tuples. However, it fails in the following code snippet.

d = {"a", "b"}
puts d.sum(&.to_s)

Expected output: ab (similar to the output of d.join)
Actual output:

In /usr/lib/crystal/enumerable.cr:1759:12

1759 | type.zero
^---
Error: undefined method 'zero' for String.class

Crystal version: 1.14.0

@rvprasad rvprasad added the kind:bug A bug in the code. Does not apply to documentation, specs, etc. label Jan 1, 2025
@rvprasad rvprasad changed the title Enumerable#sum(&) fails with heterogeneous enumerables Enumerable#sum(&) fails with certain block arguments Jan 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind:bug A bug in the code. Does not apply to documentation, specs, etc. status:discussion topic:stdlib:collection
Projects
None yet
Development

No branches or pull requests

2 participants