Records: Default values
● Intermediate2 min
A field with = value may be left out when the record is made, and then takes that value.
type User {
name: String
age: Int
city: String = "Pune"
}
fn main() {
const asha = User(name: "Asha", age: 31)
print("{asha.name}, {asha.age}, {asha.city}")
const older = asha with { age: 32 }
print("{asha.age} -> {older.age}")
let copy = asha
copy.city = "Delhi"
print("{asha.city} / {copy.city}")
}The program ran and printed:
kavach run record.kav
Asha, 31, Pune31 -> 32Pune / Delhi
city: String = "Pune" has a default, so User(name: "Asha", age: 31) is enough.