diff --git a/src/static_array.cr b/src/static_array.cr index 646b31da09a0..b8a32eff0070 100644 --- a/src/static_array.cr +++ b/src/static_array.cr @@ -62,11 +62,13 @@ struct StaticArray(T, N) # ``` # StaticArray(Int32, 3).new { |i| i * 2 } # => StaticArray[0, 2, 4] # ``` - def self.new(&block : Int32 -> T) + def self.new(& : Int32 -> T) array = uninitialized self - N.times do |i| - array.to_unsafe[i] = yield i - end + buf = array.to_unsafe + # Using a macro prevents slow compile times: https://github.com/crystal-lang/crystal/issues/2485 + {% for i in 0...N %} + buf[{{i.id}}] = yield {{i.id}} + {% end %} array end