In a contract, I have a function that returns multiple variables. It returns 7 variables but I only need one variable from it. How can I take out only the necessary one? Should I take all the variables and use only one?
interface IData {
function getData() external view returns (unit d1,
string memory d2,
address d3,
unit d4,
string memory d5,
unit d6,
address d7);
}
contract Module {
IData private keepData;
constructor(address dataAddress) {
keepData = IData(dataAddress);
}
function doSomething() external {
unit d1;
string d2;
address d3;
unit d4;
string d5;
unit d6;
address d7;
(d1, d2, d3, d4, d5, d6, d7) = keepData.getData();
// do something which only requires d5 data.
// for other 6 variables except d5, they cause unused variable warning.
}
}
If you don't want a return value you can omit it, but you still need the commas. So if you only need d5
from the returned values you can rewrite it like this:
function doSomething() external {
string d5;
(,,,,d5,,) = keepData.getData();
}