Skip to content

Add ArrayLiteral#* and StringLiteral#*#16154

Merged
straight-shoota merged 2 commits intocrystal-lang:masterfrom
jneen:feature.literal-mult
Sep 18, 2025
Merged

Add ArrayLiteral#* and StringLiteral#*#16154
straight-shoota merged 2 commits intocrystal-lang:masterfrom
jneen:feature.literal-mult

Conversation

@jneen
Copy link
Contributor

@jneen jneen commented Sep 15, 2025

To mirror Array#* and String#*. New specs added to cover these.

@ysbaddaden ysbaddaden added this to the 1.18.0 milestone Sep 16, 2025
@straight-shoota straight-shoota merged commit 05555e6 into crystal-lang:master Sep 18, 2025
41 checks passed
Comment on lines +775 to +777
it "executes *" do
assert_macro %({{["na"] * 5}}), %(["na", "na", "na", "na", "na"])
end
Copy link
Contributor

@Sija Sija Oct 11, 2025

Choose a reason for hiding this comment

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

Since the newly added functionality was placed within interpret_array_or_tuple_method, I believe we're missing tests for TupleLiteral#* case.

Comment on lines +809 to +811
# Similar to `Array#*`
def *(other : NumberLiteral) : ArrayLiteral
end
Copy link
Contributor

Choose a reason for hiding this comment

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

Since the newly added functionality was placed within interpret_array_or_tuple_method, similar comment is missing from TupleLiteral definition within the same file.

num = arg.to_number

unless num.is_a?(Int)
arg.raise "argument to StringLiteral#* cannot be a float"
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggestion: the error message could be improved as it states the problem, but not a solution. A better alternative would be explicitly stating what the expected type is.

Suggested change
arg.raise "argument to StringLiteral#* cannot be a float"
arg.raise "argument to StringLiteral#* must be an integer"

num = arg.to_number

unless num.is_a?(Int)
arg.raise "argument to ArrayLiteral#* cannot be a float"
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggestion: error message could be improved as it states the problem, but not a solution. A better alternative would be explicitly stating what the expected type is.

Suggested change
arg.raise "argument to ArrayLiteral#* cannot be a float"
arg.raise "argument to ArrayLiteral#* must be an integer"

ysbaddaden added a commit to ysbaddaden/crystal that referenced this pull request Oct 13, 2025
Also fixes some error messages.

Co-Authored-By: Sijawusz Pur Rahnama <sija@sija.pl>
straight-shoota pushed a commit that referenced this pull request Oct 14, 2025
Co-authored-by: Sijawusz Pur Rahnama <sija@sija.pl>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants