typestar

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

  1. uint256 public applesSold generates applesSold().
  2. Two named returns come back as a tuple.
  3. Named returns assign instead of writing a return line.

Keywords and builtins used here

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.

Type this snippet

Step 1 of 2 in Surfaces, step 24 of 28 in Language basics.

← Previous Next →