I was trying a simple push operation within solidity. As shown in the code below with function isArrayEven():
pragma solidity ^0.8.12;
contract Test {
uint[] public arr = [uint(1), 2, 3, 4, 5, 6, 7, 8 ,9];
function isArrayEven() public view returns(bool[] memory) {
bool[] memory ret;
for (uint i = 0; i < arr.length; i++) {
ret.push((arr[i]%2 == 0));
}
return ret;
}
}
But the following error is thrown:
Member "push" is not available in bool[] memory outside of storage.
I have figured out how to fix this by using below:
pragma solidity ^0.8.12;
contract Test {
uint[] public arr = [uint(1), 2, 3, 4, 5, 6, 7, 8 ,9];
function isArrayEven() public view returns(bool[] memory) {
bool[] memory ret = new bool[](arr.length);
for (uint i = 0; i < arr.length; i++) {
ret[i] = (arr[i]%2 == 0);
}
return ret;
}
}
But I don't understand this behavior, why is 'push' not allowed for memory arrays?