为什么从变量创建具有大小的 StaticArray 会引发错误?
Why creating StaticArray with size from a variable throws an error?
error when creating StaticArray with size from variable
我收到此错误(见图),但我不知道如何解决?
代码:
t = 3
seps = StaticArray(Int32, t).new{
2
}
seps.each{|i| p i}
错误:
Syntax error in eval:2: expecting token ')', not 't'
当我像这样初始化 StaticArray 时它起作用了:
seps = StaticArray(Int32, 3).new{
2
}
seps.each{|i| p i}
但我需要像第一个代码示例一样初始化数组!
A StaticArray 具有无法在运行时修改的固定大小。该语法甚至不允许将变量用作通用参数,因为它没有意义。
如果在运行时需要可变大小,则应改用 Array。
error when creating StaticArray with size from variable
我收到此错误(见图),但我不知道如何解决?
代码:
t = 3
seps = StaticArray(Int32, t).new{
2
}
seps.each{|i| p i}
错误:
Syntax error in eval:2: expecting token ')', not 't'
当我像这样初始化 StaticArray 时它起作用了:
seps = StaticArray(Int32, 3).new{
2
}
seps.each{|i| p i}
但我需要像第一个代码示例一样初始化数组!
A StaticArray 具有无法在运行时修改的固定大小。该语法甚至不允许将变量用作通用参数,因为它没有意义。
如果在运行时需要可变大小,则应改用 Array。