Skip to content

06 · Functions

Named argument shorthand

Calling with name: forwards the caller's name binding. Explicit arguments and shorthand can be mixed and reordered by label. A missing local binding is an error; the formatter preserves the shorthand.

On this page

Calling with name: forwards the caller's name binding. Explicit arguments and shorthand can be mixed and reordered by label. A missing local binding is an error; the formatter preserves the shorthand.

Complete example

Named argument shorthand.

20-named-argument-shorthand.zolo
Playground
// Named argument shorthand keeps the label and uses the local value.

struct User {
    name: str,
    email: str,
    active: bool,
}

fn create_user(name: str, email: str, active: bool = true) -> User {
    User { name, email, active }
}

struct Team {
    title: str,
}

impl Team {
    fn contact(self, name: str, email: str) -> str {
        "{self.title}: {name} <{email}>"
    }
}

fn main() {
    let name = "Ana"
    let email = "[email protected]"

    // Reordering and defaults are the same as for explicit named arguments.

    let user = create_user(email:, name:)
    print("{user.name} <{user.email}>") // expected: Ana <[email protected]>

    print(user.active) // expected: true


    // Explicit values and shorthand can be mixed.

    let disabled = create_user(name:, email:, active: false)
    print(disabled.active) // expected: false


    let team = Team { title: "Zolo" }
    print(team.contact(email:, name:)) // expected: Zolo: Ana <[email protected]>

}

Rules and behavior

Function parameters and lambdas · Documentation

From a checkout of the repository:

zolo examples/features/05-functions/20-named-argument-shorthand.zolo

Global index

Find your way through Zolo

Try an idea

Start here

9 results

9 results

enespt-br