Enums and records

A type whose values are all listed, the switch that answers for each of them, and a value made of named parts.

Coles 5.4 has switch, chosen by a number. This page has the two kinds of type that switch is most at home with, which Coles’ book does not cover. Both are one line at the top of the file, above the functions.

Enums

A text can be any text. When a value can only be one of a few — a day of the week, a film’s rating, a show time — an enum lists them, and those are all the values its type has:

enum Day { MON, TUE, WED, THU, FRI, SAT, SUN }

Day is now a type, like int or String: a function can take one, as in boolean isWeekend(Day d), or give one back. A value is written with the type’s name in front, Day.SAT. Anything else — Day.sat, Day.SATURDAY — Java refuses before the program runs:

error: cannot find symbol
  symbol:   variable sat
  location: class Day

With a text, isWeekend("sat") runs, and gives false.

Two values of one enum are compared with ==. There is only ever one Day.SAT, so “are these the same one?” is the right question here — as it is not for two texts, which .equals compares.

switch over an enum

A switch has one case per value, and values that share an answer share a case:

// whether d is Saturday or Sunday
boolean isWeekend(Day d) {
    return switch (d) {
        case SAT, SUN -> true;
        case MON, TUE, WED, THU, FRI -> false;
    };
}
  • Written after return, a switch is an expression, like ? :: it has a value, and return hands it out. The ; after its closing brace ends the return.

  • A case says SAT, not Day.SAT: the switch is on a Day, so Java already knows which type every case belongs to and looks the name up there. Everywhere else nothing says so, and the type’s name comes first — d == Day.SAT; a bare SAT there is cannot find symbol.

  • After each -> comes an expression of the type the function gives back. It may use the function’s other parameters: in boolean isOpenAt(Day d, int hour), a case can be case SAT, SUN -> hour >= 10;.

  • List every value, and leave out default. Then a value with no case is an error before anything runs:

    error: the switch expression does not cover all possible input values

    Every value has to reach an answer — the rule behind missing return statement, for a switch. A default would answer for any value not listed, so a forgotten value would quietly get the default’s answer.

  • Over a number, as in Coles 5.4, no list of cases can name every value, so a switch that gives a value needs a default for the rest: case 7, 11 -> …, then default -> ….

A function that takes an enum is usually one switch, with a case per value. The data has seven shapes, so the function has seven answers, or fewer when some share one.

Records

A record is a value made of named parts:

record Ride(int km, boolean night) {}

That line makes a type, Ride, whose values each hold an int called km and a boolean called night. The {} at the end stays empty in this course.

  • new Ride(3, true) makes one: new, as for a builder, with the parts in the order the line lists them.
  • r.km() and r.night() read the parts back: each part’s name, then ().
  • A record never changes — nothing sets a part after new — so, like a text, it can be treated as a value. Two records with the same parts are .equals; == asks whether they are one object, and two news make two.
  • IO.println(new Ride(3, true)) prints Ride[km=3, night=true].

A function that takes a record usually starts by reading the parts it needs, and then decides as before. A6’s nightFare, on a ride:

// the fare for a ride
int rideFare(Ride r) {
    return nightFare(r.km(), r.night());
}

Check.is(rideFare(new Ride(3, true)), 14); checks it.

In one file

The enums and records first, then the functions, then main:

enum Day { MON, TUE, WED, THU, FRI, SAT, SUN }
record Ride(int km, boolean night) {}

// whether d is Saturday or Sunday
boolean isWeekend(Day d) {
    return switch (d) {
        case SAT, SUN -> true;
        case MON, TUE, WED, THU, FRI -> false;
    };
}

// the fare: 5 up to 2 km; longer, 5 plus 2 for every km, or 3 at night
int rideFare(Ride r) {
    if (r.km() <= 2) {
        return 5;
    } else if (r.night()) {
        return 5 + r.km() * 3;
    } else {
        return 5 + r.km() * 2;
    }
}

void main() {
    Check.is(isWeekend(Day.SAT), true);
    Check.is(isWeekend(Day.FRI), false);
    Check.is(rideFare(new Ride(2, true)), 5);
    Check.is(rideFare(new Ride(3, true)), 14);
    Check.done();
}