Code below is enough to kill the compiler:
(0..).size
Infinite Iterator
s are having the same issue btw:
[1, 2, 3].cycle.size
It looks like it hangs at runtime, which I guess is expected, though we could try to detect it somehow... it's really hard, though.
Advice: always do crystal build foo.cr
and then ./foo
to see whether it's a compiler bug or just hangs at runtime.
I guess we could overload #size
in these 2 cases.
We could. It would also hang when doing map, or select or any of the other enumerable methods.
could we have another class called InfiniteIterator
subclassed from Iterator
for cases like this?
This is expected behaviour. Every language will hang if you try and perform an infinite amount of work. What would #size
return? Infinity
isn't an integer.
For this specific case, Range#size
could at least raise when end
is nil
.
It would probably make sense to override this method anyway, because for the most common use case with integers, the size can be calculated directly. It doesn't need to iterate through the elements as Enumerable#size
does.
It would be nice to have an Infinity
to return. I think Float
is the object that has an infinity but maybe the Int
classes should have one for cases like this.
@wontruefree IMO if anything Infinity
should be moved into the Number
struct instead.
@wontruefree Int
has no concept of infinity, so it can't be a valid return value. And it's not really useful here either.
Most helpful comment
Advice: always do
crystal build foo.cr
and then./foo
to see whether it's a compiler bug or just hangs at runtime.