Crystal: Int#times on types other than Int32

Created on 22 Sep 2017  路  1Comment  路  Source: crystal-lang/crystal

The code

4_u32.times.to_a

leads to the compilation error

Error in line 1: instantiating 'Int::TimesIterator(UInt32)#to_a()'

in /opt/crystal/src/enumerable.cr:1278: instantiating 'each()'

    each { |e| ary << e }
    ^~~~

in /opt/crystal/src/enumerable.cr:1278: instantiating 'each()'

    each { |e| ary << e }
    ^~~~

in /opt/crystal/src/enumerable.cr:1278: no overload matches 'Array(UInt32)#<<' with type Int32
Overloads are:
 - Array(T)#<<(value : T)

    each { |e| ary << e }
                   ^~

The problem seems to be in TimesIterator. This class has a type argument T and includes Iterator(T), which seems to expect a next method that generally returns values of type T. However, TimesIterator#next returns Int32 values regardless of T.

I am running Crystal 0.23.1 [e2a1389] (2017-07-13) LLVM 3.8.1.

bug stdlib

Most helpful comment

@asterite would something like the following work?

diff --git a/src/int.cr b/src/int.cr
index 22ce0da..4c52a32 100644
--- a/src/int.cr
+++ b/src/int.cr
@@ -499,9 +499,9 @@ struct Int
     include Iterator(T)

     @n : T
-    @index : Int32
+    @index : T

-    def initialize(@n : T, @index = 0)
+    def initialize(@n : T, @index = T.zero)
     end

     def next

On my local test that pass, but I might be missing something. Thank you.

>All comments

@asterite would something like the following work?

diff --git a/src/int.cr b/src/int.cr
index 22ce0da..4c52a32 100644
--- a/src/int.cr
+++ b/src/int.cr
@@ -499,9 +499,9 @@ struct Int
     include Iterator(T)

     @n : T
-    @index : Int32
+    @index : T

-    def initialize(@n : T, @index = 0)
+    def initialize(@n : T, @index = T.zero)
     end

     def next

On my local test that pass, but I might be missing something. Thank you.

Was this page helpful?
0 / 5 - 0 ratings