Skip to main content

Trailing struct parameters

If the last parameter of a function is a struct, then you can pass its fields directly.

main.w
// struct for the function params
struct NameOptions {
formal: bool;
caps: bool;
}

let greet = (name: str, options: NameOptions) => {
let var prefix = "Hi, ";
if options.formal {
prefix = "Greetings, ";
}
let var message = "{prefix}{name}";
if options.caps {
message = message.uppercase();
}
log(message);
};

greet("kermit", NameOptions { formal: true, caps: false });

// Pass fields directly as the last param is a Struct
greet("kermit", formal: true, caps: false);

Wing console output
# Run locally with wing console
wing it

Greetings, kermit
Greetings, kermit