Default Values
The syntax schema Name { field: Type = default } associates a default value
with each field. Calling Name.default() returns an instance with all fields
filled in — no arguments, no map, no validation. It is the right choice for
configurations and request objects where "the initial state is already
meaningful".
Config.default() fills in port, host, and debug mode without any arguments.
01-defaults.zolo
// Feature: schema with default values — `Schema.default()` builds an instance
// Syntax: `schema Name { field: type = default, ... }`. Calling
// `Name.default()` returns an instance with every field set.
// When to use: configs, settings, request bodies — when "the default
// shape is meaningful and you want zero-arg construction".
schema Config {
port: int = 8080,
host: str = "localhost",
debug: bool = false,
}
let cfg = Config.default()
print(cfg.port)
// expected: 8080
print(cfg.host)
// expected: localhost
print(cfg.debug)
// expected: false
// Fields are accessed like struct fields.
print("listening on {cfg.host}:{cfg.port}")
// expected: listening on localhost:8080
Challenge
Add a field timeout: int = 30 to the Config schema and print
cfg.timeout. What happens if you remove the default value and call
Config.default() again?
See also