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
OpenCL C
Pascal
Pony
Python
Racket
Ruby
Rust
Snowball
Scala
Solidity
Spice
SPIR-V
Swift
LLVM TableGen
Toit
TypeScript Native
V
Vala
Visual Basic
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"); const MyApp = App(i32); fn myLoad(context: *MyApp.Context) !void { std.debug.warn("load: {}\n", .{context.model}); context.model += 1; } fn myUnload(context: *MyApp.Context) void { std.debug.warn("load: {}\n", .{context.model}); } pub fn main() !void { var app = try MyApp.init(.{ .allocator = null, .model = 42, .load = myLoad, .unload = myUnload, }); defer app.deinit(); } pub fn AppContext(comptime T: type) type { return struct { const Self = @This(); // Set by user. allocator: ?*std.mem.Allocator, model: T, load: fn(context: *Self) anyerror!void, unload: fn(context: *Self) void, // Internal. err: ?anyerror = null, pub fn foo(self: *Self, yes: bool) void { } }; } pub fn AppConfig(comptime Context: type, comptime Model: type) type { return struct { allocator: ?*std.mem.Allocator, model: Model, load: ?fn(context: *Context) anyerror!void = null, unload: ?fn(context: *Context) void = null, }; } pub fn App(comptime Model: type) type { return struct { const Self = @This(); pub const Context = AppContext(Model); pub const Config = AppConfig(Context, Model); context: Context, pub fn init(config: Config) !Self { var self = Self{ .context = .{ .allocator = config.allocator, .model = config.model, .load = config.load orelse noopLoad, .unload = config.unload orelse noopUnload, }, }; try self.context.load(&self.context); return self; } pub fn deinit(self: *Self) void { self.context.unload(&self.context); } pub fn run(self: *Self) !void { while (true) { // event loop } } fn noopLoad(ctx: *Context) !void { } fn noopUnload(ctx: *Context) void { } }; }
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
About the author
Statistics
Changelog
Version tree