Thanks for using Compiler Explorer
Sponsors
Jakt
C++
Ada
Analysis
Android Java
Android Kotlin
Assembly
C
C3
Carbon
C++ (Circle)
CIRCT
Clean
CMake
CMakeScript
COBOL
C++ for OpenCL
MLIR
Cppx
Cppx-Blue
Cppx-Gold
Cpp2-cppfront
Crystal
C#
CUDA C++
D
Dart
Elixir
Erlang
Fortran
F#
GLSL
Go
Haskell
HLSL
Hook
Hylo
IL
ispc
Java
Julia
Kotlin
LLVM IR
LLVM MIR
Modula-2
Nim
Objective-C
Objective-C++
OCaml
Odin
OpenCL C
Pascal
Pony
Python
Racket
Ruby
Rust
Snowball
Scala
Slang
Solidity
Spice
SPIR-V
Swift
LLVM TableGen
Toit
TypeScript Native
V
Vala
Visual Basic
Vyper
WASM
Zig
Javascript
GIMPLE
Ygen
zig source #1
Output
Compile to binary object
Link to binary
Execute the code
Intel asm syntax
Demangle identifiers
Verbose demangling
Filters
Unused labels
Library functions
Directives
Comments
Horizontal whitespace
Debug intrinsics
Compiler
zig 0.10.0
zig 0.11.0
zig 0.12.0
zig 0.12.1
zig 0.13.0
zig 0.2.0
zig 0.3.0
zig 0.4.0
zig 0.5.0
zig 0.6.0
zig 0.7.0
zig 0.7.1
zig 0.8.0
zig 0.9.0
zig trunk
Options
Source code
const std = @import("std"); // Create a tuple with one runtime-typed field fn UniTuple(comptime T: type) type { const Hack = struct { var forced_runtime: T = undefined; }; return @TypeOf(.{Hack.forced_runtime}); } /// Types should be an iterable of types fn Tuple(comptime types: anytype) type { const H = struct { value: anytype, }; var empty_tuple = .{}; var container = H{ .value = empty_tuple, }; for (types) |T| { container.value = container.value ++ UniTuple(T){ .@"0" = undefined }; } return @TypeOf(container.value); } pub fn StructType2(comptime names: []const []const u8, comptime types: anytype) type { std.debug.assert(names.len == types.len); const Storage = Tuple(types); return struct { const field_names = names; const field_types = types; const Self = @This(); storage: Storage, pub fn create(literal: anytype) Self { comptime std.debug.assert(std.meta.fields(@TypeOf(literal)).len == field_names.len); comptime std.debug.assert(std.meta.trait.hasFields(@TypeOf(literal), field_names)); var self: Self = undefined; inline for (field_names) |name, idx| { self.storage[idx] = @field(literal, name); } return self; } fn fieldIndex(comptime name: []const u8) ?comptime_int { var i = 0; while (i < field_names.len) : (i += 1) { if (std.mem.eql(u8, name, field_names[i])) return i; } return null; } fn FieldType(comptime name: []const u8) type { const idx = fieldIndex(name) orelse @compileError("Field '" ++ name ++ "' not in struct '" ++ @typeName(Self) ++ "'."); return field_types[idx]; } pub fn field(self: anytype, comptime name: []const u8) if (@TypeOf(self) == *Self) *FieldType(name) else *const FieldType(name) { const idx = fieldIndex(name).?; return &self.storage[idx]; } pub fn format( self: Self, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype, ) !void { try writer.writeAll(@typeName(Self)); if (std.fmt.default_max_depth == 0) { return writer.writeAll("{ ... }"); } try writer.writeAll("{"); inline for (field_names) |f, i| { if (i == 0) { try writer.writeAll(" ."); } else { try writer.writeAll(", ."); } try writer.writeAll(f); try writer.writeAll(" = "); try std.fmt.formatType(self.storage[i], fmt, options, writer, std.fmt.default_max_depth - 1); } try writer.writeAll(" }"); } }; } pub fn StructType(comptime fields: anytype) type { var field_names: [fields.len][]const u8 = undefined; var field_types: [fields.len]type = undefined; for (fields) |f, idx| { field_names[idx] = f[0]; field_types[idx] = f[1]; } return StructType2(&field_names, &field_types); } pub fn main() void { const FooStruct = StructType(.{ .{ "a", usize }, .{ "b", bool }, }); var foo = FooStruct.create(.{ .a = 0, .b = false }); foo.field("a").* = 1; std.debug.print("{}\n", .{foo}); }
Become a Patron
Sponsor on GitHub
Donate via PayPal
Source on GitHub
Mailing list
Installed libraries
Wiki
Report an issue
How it works
Contact the author
CE on Mastodon
CE on Bluesky
About the author
Statistics
Changelog
Version tree