crystal-lang / crystal

The Crystal Programming Language
https://crystal-lang.org
Apache License 2.0
19.22k stars 1.61k forks source link

Fix `Range#size` return type to `Int32` #14588

Open straight-shoota opened 1 month ago

straight-shoota commented 1 month ago

Resolves #14587

The super implementation Enumerable#size has the same type restriction already.

straight-shoota commented 1 month ago

Hm, actually this can overflow on ranges of Int types that are bigger than Int32 🤔 Should we only cast to Int32 on smaller types?

beta-ziliani commented 1 month ago

I'm afraid there's no good solution here. The good part of raising on overflow is that if it fits, it works. The bad part is that the failure is at runtime...

An alternative, as good or bad, is to statically fail on > Int32 types and have people do the math themselves 🤷