Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Attributes

An attribute is a name in square brackets that tells the compiler something about the declaration below it. Each attribute goes on its own line, above its target, as pep-fmt writes it, except a parameter's, which stay on its line, in front of it:

# Attributes, each on its own line above what it applies to.

[Target(OS(Windows))]
system_name(): String {
    return "Windows";
}

[Target(!OS(Windows))]
system_name(): String {
    return "not Windows";
}

[Deprecated("use 'system_name' instead", "system_name")]
os_name(): String {
    return system_name();
}

# A parameter's attributes stay on its line, in front of it.
keep([Move] names: Array<String>): ulong {
    return names.count;
}

main(): int {
    print(system_name().size > 0);
    var names = ["a", "b"];
    print(keep(move(names)));
    return 0;
}

# Output:
# true
# 2

The built-in attributes are each covered in the chapter for what they do:

AttributeGoes onChapter
[Value]a struct or enumValue structs, value enums
[Equatable]a struct or enumDeriving == and hash
[Implicit]a constructorImplicit conversions
[Alias], [Rename]a parameterOther names for a parameter
[Shared]a structWhat threads can share
[ThreadLocal]a module-scope varThread-local variables
[Isolated]a structWhat a lock body may do
[Move], [MoveSource]a parameterHanding a reference over with move
[NotCopyable], [Locked]a struct; a parameterthe reference
[Unsafe]a functionRemoving the checks
[Likely], [Unlikely]an if or a match armBranch hints
[Cold]a functionCold functions
[Target]most declarations, and blocksPlatform selection
[BuildOption]a declaration or a blockBuild options
[Link]an extern functionLinking
[Export]a functionCalling Pepper from C
[Test], [ShouldPanic]a functionUnit tests
[Deprecated]most declarationsDeprecation
[Attribute]a structAttributes of your own
[Name], [Ignore], [Required], [Tag], [Untagged]a property; an enum or its casesJSON: attributes (the serialize module)
[cli.Option], [cli.Positional]a fieldDeclaring options as a struct

A struct marked [Attribute] is an attribute of your own, which a static block reads at compile time (see Reflection and attributes). The built-in ones are declared the same way, in the runtime, so the reference lists each with its fields.