Skip to content

Make all overloads of ArrayLiteral#[] return nil on out of bounds#16453

Merged
straight-shoota merged 1 commit intocrystal-lang:masterfrom
HertzDevil:feature/arrayliteral-indexer-bounds
Dec 11, 2025
Merged

Make all overloads of ArrayLiteral#[] return nil on out of bounds#16453
straight-shoota merged 1 commit intocrystal-lang:masterfrom
HertzDevil:feature/arrayliteral-indexer-bounds

Conversation

@HertzDevil
Copy link
Contributor

The single-index overload in the macro language has always behaved like #[]? in non-macro code, so I don't think there is any particular benefit in making both the range overload and the start + count overload both raise on out-of-bounds start indices.

This actually applies to TupleLiteral as well.

@straight-shoota
Copy link
Member

Sorry, this PR slipped through my review list.

The proposal sounds good. I suppose the rule of thumb is that methods in macro land should generally behave similar to the equivalents in Ruby.
Like Ruby, there is no static typing.

Even more, in macro land we couldn't even rescue an exception. So raising is not very useful except for really unrecoverable errors. It aborts the entire compilation. Returning NilLiteral has the same effect as raising when the calling code does not expect it.

@straight-shoota straight-shoota added this to the 1.19.0 milestone Dec 9, 2025
@straight-shoota straight-shoota merged commit 1136464 into crystal-lang:master Dec 11, 2025
42 checks passed
@HertzDevil HertzDevil deleted the feature/arrayliteral-indexer-bounds branch December 24, 2025 01:24
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.

3 participants