Comments (1)
Quoting PR comments to bring the general discussion back here:
@straight-shoota #14588 (comment)
Hm, actually this can overflow on ranges of
Int
types that are bigger thanInt32
🤔
Should we only cast toInt32
on smaller types?
@beta-ziliani #14588 (comment)
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 🤷
That's also not good because (0_u128..1_u128).size
is perfectly valid 🤷
I think casting to Int32
is fine. If you use bigger number types, it will raise but that's ok. The implementation with -
for Int
types is just an optimization. The base implementation of Enumerable#size
would iterate all the items and thus eventually overflow the counter if the difference is more than Int32
can handle.
The type of #size
for all collection types is Int32
because it's meant for collections that can be feasibly represented in memory.
We'll eventually need to increase that size type in order to support bigger collections. But while Int64
should be entirely sufficient for the use case related to actual memory storage (e.g. Slice#size
), Range#size
is not materialized and can reach arbitrary scales (with BigInt
etc.).
#size
is simply not meant for this.
I think we should explain that caveat explicitly in the API docs and that's it. #size
returns Int32
and if it doesn't fit, it will raise.
We should however consider adding an alternative method that unconditionally returns range.end - range.begin
.
This would even be much more versatile because it would work not just with Int
but any type that implements subtraction (e.g. 0_f32..1_f32
, Time.utc..Time.utc
).
from crystal.
Related Issues (20)
- `Indexable#find` and `#find!` with start offsets
- False positive for "Recursive struct detected" HOT 2
- Struct#pretty_print Sorts Fields by Name HOT 6
- Hex Array Literals HOT 4
- Case insensitive comparison through Quality of Life method `String#=~(other : String)` HOT 4
- `unsafe_as` should be a compiler primitive HOT 4
- Remove `Pointer.new(Int)` HOT 4
- UTF-16 string literals HOT 13
- Non-atomic memory is cleared twice HOT 3
- `GC.malloc` doesn't clear memory (as specified) for `gc_none`
- [RFC] Add `URI::Params::Serializable`
- UTF-8 support in Windows API HOT 1
- Non-zero padded exponent in float string representation after Ryu implementation HOT 6
- Allocation of non-zeroed memory for performance boost HOT 10
- Crystal::Macros::Arg#restriction method always returns Nop HOT 1
- Nilable `Proc` types inside libs
- Cannot return `Proc`s from top-level funs
- `ReferenceStorage(T)` is always atomic even when `T` isn't HOT 1
- Add `crystal tool method_types` for listing method parameter types HOT 4
- Passing nil to Addrinfo.getaddrinfo gives unexpected error message HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from crystal.