Getters and tuples in Solidity
public state writes its own getter; functions return tuples.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
// public state writes its own getter; functions return tuples.
contract StandTally {
uint256 public applesSold; // applesSold() comes free
uint256 public cidersSold;
function ring(uint256 apples, uint256 ciders) external {
applesSold += apples;
cidersSold += ciders;
}
function totals()
external view returns (uint256 apples, uint256 ciders)
{
apples = applesSold;
ciders = cidersSold; // named returns need no return line
}
}
How it works
uint256 public applesSoldgeneratesapplesSold().- Two named returns come back as a tuple.
- Named returns assign instead of writing a return line.
Keywords and builtins used here
contractexternalfunctionpublicreturnsuint256
The run, in numbers
- Lines
- 20
- Characters to type
- 490
- Tokens
- 66
- Three-star pace
- 60 tpm
At the three-star pace of 60 tokens a minute, this run takes about 66 seconds.
Step 1 of 2 in Surfaces, step 24 of 28 in Language basics.