Solidity – How to Return a Tuple Element in Remix

remixsolidity

I have the following function with a tuple:

function getETHPrice() internal view returns (int) {
    (,int price,,,) = AggregatorV3Interface(ETH_USD_FEED).latestRoundData();
    return price;
}

How do I return the price without storing it in an intermediary variable?
In another language, I would do something like this:

return AggregatorV3Interface(ETH_USD_FEED).latestRoundData()[1]

How can i do that?
When I try that, I get the following error:

TypeError: Indexed expression has to be a type, mapping or array (is tuple(uint80,int256,uint256,uint256,uint80))

Best Answer

In the Solidity official documentation there is a part about tuples

Tuples are not proper types in Solidity, they can only be used to form syntactic groupings of expressions.

You can use it as a return value but can't access a certain element in it. The way you do it in your code is the way to go, unfortunately. You could simplify it a bit by declaring a return variable like this if you would like:

function getETHPrice() internal view returns (int price) {
    (, price,,,) = AggregatorV3Interface(ETH_USD_FEED).latestRoundData();
}