I’m learning about Iterators and getting a compilation error related to a struct’s visibility that doesn’t make sense to me, although I’m a Crystal newbie so perhaps my expectations are off. Consider the following code:
class Thingy
include Iterator(Int32)
def initialize()
@x = 0
@y = Slice(Int32).new(5)
end
def next
return stop if @x >= 3
@x += 1
end
end
z = Thingy.new
z.each { |i| puts i }
This is a simple iterator that shold print out the numbers
1
2
3
The instance variable @y has no purpose in this example, but it does have an effect: it results in the compiler error
In test.cr:6:10
6 | @y = Slice(Int32).new(5)
^
Error: wrong number of type vars for Iterator::Slice(I, T, N) (given 1, expected 3)
Apparently the definition of Slice
is being overwritten by Iterator. Checking the code, the redefinition is supposed to be private
but is nevertheless visible to the body of Thingy
. Is this intentional/expected?
(Note that replacing Slice
above with ::Slice
fixes the problem and leads to the expected output.)