Record fields can have defaults

A field with a default can be left out. The default runs every time a value is built.

A field can carry a value for when the caller leaves it out. Both construction forms honour it:

type User = User { name: String, age: Int = 0 }

User("Sean")
User { name: "Sean" }
User("Ada", 36)

The first two build a user whose age is 0. The third passes 36, and 36 is what you get. An explicit argument wins.

The default has to be last

This declaration is refused:

type User = User { name: String, age: Int = 0, city: String }

city has no default, and it sits after age, which does. User("Sean") would then be ambiguous: the missing piece might be the age or the city. The parser stops at the type, with:

field 'city' has no default but follows 'age', which does - fields with defaults must come last

Several defaults in a row are fine, as long as every one of them is at the end. name: String, age: Int = 0, city: String = "Oslo" is a legal User.

It runs on every value

The expression is not folded once when the type is declared. Two values never share one result:

fn starting_credits() = 25

type Trial = Trial { owner: String, credits: Int = starting_credits() }

a = Trial("Ada")
b = Trial("Grace")

starting_credits runs for a and again for b. That is what you want when the default is a timestamp, a counter, or anything else that should be fresh. It is also why a default that builds a list gives each record its own list.

The expression is resolved where the type was written. A function in that scope works. A builtin works. A local let or mut does not:

fn seven() = 7

type Fine = Fine { n: Int = abs(-1) }
type Also = Also { n: Int = seven() }
fn bad() {
    let n = 1
    type Oops = Oops { k: Int = n }
}

ecko check reports n as an undefined variable. The type's scope does not include the locals around it. If the default needs a number you computed, wrap that computation in a function and call the function.

A field with no default is still required. Leave name out of User and the value fails the declared type, the same as it did before defaults existed.

The reference page is type definitions.