Attributes

Built-in attributes

AttributeTargetEffect
@profile(t)fileSet strictness profile (t0, t1, t2)
@link("lib")moduleDeclare native library dependency
@export / @export("sym")fnExport with C calling convention
@inlinefnInline function at call sites
@swizzle pattern: TypestructEnable vector swizzle access
@embed("path")expressionEmbed file contents as string (comptime)

Built-in comptime intrinsics

These @name(...) forms are available in expressions (and in @tape blocks):

IntrinsicReturnsDescription
@sizeof(T)i64Size of type in bytes
@alignof(T)i64Alignment of type
@typeof(expr)typeType of expression
@has_field(T, name)boolWhether struct has a field
@has_method(T, name)boolWhether type has a method
@inner_type(T)typeInner type of optional/slice
@field_type(T, name)typeType of a struct field
@assert(cond)voidCompile-time assertion
@embed("path")[]const u8Embed file at compile time
@print(...)voidPrint at compile time
@println(...)voidPrint with newline at compile time
@compile_error(msg)voidEmit compile error

User-defined attributes

Declare custom attributes with the attr keyword:

tape
attr serializable;
attr deprecated(message: []const u8);

@serializable
struct Config {
    name: []const u8;
    value: i64;
}

@deprecated("use new_api() instead")
pub fn old_api() -> i64 {
    return new_api();
}

User attributes can be attached to functions, structs, struct fields, and tagged union variants. They accept up to 16 arguments. Attribute names cannot shadow built-in names (profile, link, export, inline, tape, emit, packed, opaque, align, sizeof, alignof, assert, typeof, print, println, compile_error).

Qualified attributes

Attributes from other modules use dot syntax:

tape
@mylib.serializable
struct Data { x: i64; }

Compile-time attribute access

In @tape blocks, use .has_attr() on comptime struct values to check for attributes:

tape
@tape fn gen_serialize(T: type) {
    if (T.has_attr(serializable)) {
        @emit fn serialize(self: *T) -> []u8 {
            // generated serialization
        }
    }
}

Last modified: