Attributes
Built-in attributes
| Attribute | Target | Effect |
|---|---|---|
@profile(t) | file | Set strictness profile (t0, t1, t2) |
@link("lib") | module | Declare native library dependency |
@export / @export("sym") | fn | Export with C calling convention |
@inline | fn | Inline function at call sites |
@swizzle pattern: Type | struct | Enable vector swizzle access |
@embed("path") | expression | Embed file contents as string (comptime) |
Built-in comptime intrinsics
These @name(...) forms are available in expressions (and in @tape blocks):
| Intrinsic | Returns | Description |
|---|---|---|
@sizeof(T) | i64 | Size of type in bytes |
@alignof(T) | i64 | Alignment of type |
@typeof(expr) | type | Type of expression |
@has_field(T, name) | bool | Whether struct has a field |
@has_method(T, name) | bool | Whether type has a method |
@inner_type(T) | type | Inner type of optional/slice |
@field_type(T, name) | type | Type of a struct field |
@assert(cond) | void | Compile-time assertion |
@embed("path") | []const u8 | Embed file at compile time |
@print(...) | void | Print at compile time |
@println(...) | void | Print with newline at compile time |
@compile_error(msg) | void | Emit 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: