Comments (9)
IMO there is no choice. The return type of any implementation of Enumerable#size
must be Int32
.
If we want a method to return anything else, it needs a different name.
from crystal.
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.
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).
But how would this work if the range is exclusive?
I'm thinking that the only reason why the untyped #size
method doesn't work is just because of the need to return the 0. Perhaps the real solution here is to use macros first to decide statically what to do with Int
, and a method to cast an Int
to another using the type. What I'm thinking is something like a Int#cast_to(Type)
, but it's not simple to implement. Then we can cast the 0 to the right type.
from crystal.
Of course range.end - range.begin
in general only works with an inclusive range. Perhaps we can have a special case for Int
.
I don't follow what you mean in the rest of the comment about the type of 0
value. Why is that an issue?
from crystal.
If you do:
if bla
0
else
a - b
end
the result is the union of Int32
and whatever a
and b
are. This is why we get UInt8 | Int32
. If we have a way to say "return 0, but casted to typeof(b)", then we drop the union and get the specific type.
from crystal.
Okay, gotcha. But the problem is not the union alone. Returning anything but Int32
breaks the contract of Enumerable#size : Int32
.
So there's no point in converging the return type on UInt8
. It needs to be Int32
.
(having 0
typed identically to the difference should be possible though with something like typeof(a - b).new(0)
; it just doesn't help anything)
from crystal.
@beta-ziliani We can do typeof(n).zero
, but since we fallback to Enumerable#size
for other cases then the returned type is still an union. We can use indeed use macros and the returned type will be typeof(B)
(when B and E are integers):
{% if B < Int && E < Int %}
...
n < 0 ? typeof(n).zero : n
{% else %}
...
{% end %}
The return type of Range(B, E)#size
still won't match Enumerable#size
, and it will be any integer type that B happens to be. At the very least it won't be an Union, and we could cast it to be at least an Int32.
I think range.begin - range.end
is basically #14740.
from crystal.
ah yes, when I wrote this comment I was aware of the problem with the interface in Enumerable#size
and now I forgot 🙈 Sorry for the noise.
from crystal.
TBH: I'm not sure which solution is best. I think both choices have pros and cons:
stable type but overflows vs unstable type but no overflow 🤷
from crystal.
Related Issues (20)
- Setting instance variable of file-private type in def parameter breaks with restriction augmenter
- Error: can't restrict Array(A | B) to Array(A | B)
- Truncated quotient and remainder
- Loading debug symbols is not thread-safe on Windows HOT 2
- Inverse of `Int#digits`
- `Number#humanize` raises on infinity and NaN
- `bin\crystal run` wrapper does not terminate if interrupts are ignored HOT 4
- `Object#crystal_type_id` returns different value for virtual type in interpreter
- Typing of ivars does not play well with macros HOT 1
- Reduce CI usage
- Crystal compiler error: Element not found (Enumerable::NotFoundError)
- Crystal Compiler error: Module validation failed: GEP base pointer is not a vector or a vector of pointers HOT 1
- Incorrect function args when yield used in argument list HOT 3
- Invalid memory access (signal 11) at address 0x20
- bsearch return unexpected value when using less or equal test HOT 1
- Native text encoding conversions HOT 4
- Exclude the execution of specific examples in `crystal spec`
- OpenSSL 1.1.1 is broken on aarch64-linux-gnu
- Reusing AST nodes in macro `#each` HOT 1
- Bug when assigning variable inside block in while 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.