[Ethereum] Solidity. issue with fixed-sized arrays and dynamic arrays

arrayssolidity

I am passing a fixed array to a function where a dynamic array is expected.

addType([1,5, 2,5, 3,2, 4,2, 5,1], [6,1], [7,5]);
addType([1,5], [6,1, 2,5, 3,3], [7,5]);

function addType (
    uint8[] memory _build,
    uint8[] memory _input,
    uint8[] memory _output
)
.......

I get an error when compiling

Invalid type for argument in function call. Invalid implicit conversion from uint8[10] memory to uint8[] memory requested

Found a solution in using a similar structure

uint8[] memory _build = new uint8[](10);
    _build[0] = 1;
    _build[1] = 5;
    _build[2] = 2;
    _build[3] = 5;
    _build[4] = 3;
    _build[5] = 2;
    _build[6] = 4;
    _build[7] = 2;
    _build[8] = 5;
    _build[9] = 1;
uint8[] memory _input = new uint8[](2);
    _input[0] = 6;
    _input[1] = 1;
uint8[] memory _output = new uint8[](2);
    _output[0] = 7;
    _output[1] = 5;

addType(_build, _input, _output);

But it is extremely not effective!

Help, please deal with these arrays!

Best Answer

Function addType expects dynamic storage arrays here while you implicitly create fixed-length when passing just [1,2,3].

Try creating explicit dynamic storage array, push there some values and then pass to function.

Good tutorial on topic: https://github.com/willitscale/learning-solidity/blob/master/support/INVALID_IMPLICIT_CONVERSION_OF_ARRAYS.MD

Related Topic