Skip to content
Records

Records

A score means little without the name of the player who made it. A record keeps several values together under one type. Start a new file, scores.qdx.

record result { player: bytes, points: int }

fn better(a: result, b: result) -> result {
  if b.points > a.points { b } else { a }
}

fn main() -> int {
  let mira = result(player: "mira", points: 17);
  let tomas = result(player: "tomas", points: 16);
  print(concat(better(mira, tomas).player, " wins\n"))
}
bootstrap/qdx run std/*.qdx scores.qdx
mira wins

Declaring one

record result { player: bytes, points: int } declares a new type named result with two fields. Declarations sit at the top level of a file, next to functions, and can appear in any order.

Making one

A record is made by writing its name like a function call, with every field named and in the order they were declared. result(player: "mira", points: 17) is the only way to write that value. Leaving a field out, changing the order, or dropping the names is an error.

Reading a field

A dot reads a field: b.points, or better(mira, tomas).player straight from a function’s result.

What a record cannot do

A record cannot be changed. To get Mira with more points, make a new record. Two records also cannot be compared with ==. Compare the fields you care about instead.

A record always has every one of its fields. Sometimes a value is one thing or another, such as a line that holds a score or is empty. The next chapter is about that.