'Error in solidity code: Return argument type bytes1 is not implicitly convertible to expected type
I have setter and getter:
function setFunc(bytes calldata _value) public {
getBytes = _value;
}
function getFunc() public view returns(bytes calldata) {
return getBytes;
}
When I run my code, the compiler shows that
TypeError: Return argument type bytes1 is not implicitly convertible to expected type (type of first return variable) bytes calldata.
--> contracts/GetterSetter.sol:38:16:
|
38 | return getBytes[];
| ^^^^^^^^^^
Before that I had another error:
TypeError: Data location must be "memory" or "calldata" for return parameter in function, but none was given.
--> contracts/GetterSetter.sol:37:51:
|
37 | function requestedBytes() public view returns(bytes) {
| ^^^^^
Could you help to resolve it and what input example should I provide into setter for correct functions working?
I will be really appreciate for your help!
Solution 1:[1]
The getter function return type needs to have the memory data location.
It's because the EVM loads the value from storage to memory, and then returns it from memory.
pragma solidity ^0.8;
contract MyContract {
bytes getBytes;
function setFunc(bytes calldata _value) public {
getBytes = _value;
}
function getFunc() public view returns(bytes memory) {
return getBytes;
}
}
The return type could have the calldata location only if it's returning the actual and unchanged calldata from the input param:
pragma solidity ^0.8;
contract MyContract {
function getFunc(bytes calldata _value) public pure returns(bytes calldata) {
return _value;
}
}
Solution 2:[2]
Hm.. I tried in Remix and it looks like woks as it should, but when I run it with Hardhat, it returns Zero.
await getset.setBytes32('0x7465737400000000000000000000000000000000000000000000000000000000');
const getBytes32 = await getset.requestedBytes32();
console.log('Bytes32:', getBytes32);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Petr Hejda |
| Solution 2 | Vlad |
