The @"something"
syntax is mostly commonly used when the name of a field matches that of a keyword in the language. std.builtin.Type
, for example, has quite a few:
switch (@typeInfo(T)) {
.@"struct", .@"fn", .@"union", .@"enum", .@"opaque" => {
// these are key word
},
.array, .pointer, .comptime_int, .int, .float, .error_set => {
/// these are not
},
}
Enum names with white-spaces are useful in situations where you need human-readable text. The best example is HTTP status codes:
enum {
// ...
@"I'm a teapot" = 418,
@"Enhance Your Calm" = 420,
// ...
};
When constructing the response header, you just need to do a @tagName(status)
to obtain the necessary text. No messing with look-up tables.
Special field names (that require the use of `@“…”) are sometimes used to denote fields with special significance. I’ve seen people do this:
const SomeStruct = struct {
name: []const u8,
comptime @"meta(name)": Metadata = .{ ... },
age: [] u32,
comptime @"meta(age)": Metadata = .{ ... },
// ...etc
};
Another potential use is the caching of complex results:
const DataPoint = struct {
a: f64,
b: f64,
@"@sin(a) + @sqrt(b) + 0.88": f64,
};
Here there’s really no good name for the field in this case. The actual expression best conveys what it is.