我很难以一种优雅的方式创建uint16[]数组。
我试试看:
function init() public {
uint16[] memory x = [uint16(1), uint16(1)];
test(x)
}这会引发TypeError: Type uint16[2] memory is not implicitly convertible to expected type uint16[]
我想在
function test(uint16[] input){}函数
当然,我可以创建一个uint16[]数组及其中的push值,但这并不是初始化它的一种优雅方法。如何正确初始化这些值?
发布于 2018-04-25 23:25:10
对于当前版本的Solidity (v.0.4.21),不能将固定大小的内存数组分配给动态大小的内存数组。
查看Array Literals / Inline Arrays在坚实性文件中的文档。
下列代码不起作用:
// This will not compile.
pragma solidity ^0.4.0;
contract C {
function f() public {
// The next line creates a type error because uint[3] memory
// cannot be converted to uint[] memory.
uint[] x = [uint(1), 3, 4];
}
}文件上说:
计划在将来取消这一限制,但由于数组是如何在ABI中传递的,因此目前会造成一些复杂情况。
https://ethereum.stackexchange.com/questions/46715
复制相似问题