3

error when creating StaticArray with size from variable

I get this error (see image) but I don't know how to resolve this ?

code :

t = 3
seps = StaticArray(Int32, t).new{
      2
}
seps.each{|i| p i}

error :

Syntax error in eval:2: expecting token ')', not 't'

It works when I initialize the StaticArray like this :

seps = StaticArray(Int32, 3).new{
      2
}
seps.each{|i| p i}

But I need to init the array like the first sample of code !

Mead
  • 100
  • 10

1 Answers1

4

A StaticArray has a fixed size that can't be modified at runtime. The syntax doesn't even allow to use a variable as a generic argument because it wouldn't make sense.

If you need variable size at runtime, you should use Array instead.

Johannes Müller
  • 5,581
  • 1
  • 11
  • 25