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"); pub fn ErrorDispatcher(tok: var) type { var curr_set: type = void; return struct { const set_ptr = &curr_set; const Self = @This(); err: anyerror, pub inline fn init(err: var) !Self { if (comptime !std.meta.trait.is(.ErrorSet)(@TypeOf(err))) @compileError("Value should be an error"); comptime { curr_set = @TypeOf(err); } return Self{ .err = err }; } pub inline fn case(self: *Self, comptime SubSet: type) !void { if (comptime !std.meta.trait.is(.ErrorSet)(SubSet)) { @compileError("Sub set needs to be an error set..."); } if (@typeInfo(curr_set).ErrorSet) |CurrentSet| { comptime { var found_fields = [1]bool {false} ** CurrentSet.len; for (std.meta.fields(SubSet)) |sub_field| { var found = false; for (CurrentSet) |field, idx| { if (comptime std.mem.eql(u8, field.name, sub_field.name)) { found_fields[idx] = true; found = true; break; } } if (!found) @compileError("Subset field " ++ sub_field.name ++ " not in current set."); } // Make new narrowed type var errors: []const std.builtin.TypeInfo.Error = &[0]std.builtin.TypeInfo.Error{}; for (CurrentSet) |field, idx| { if (!found_fields[idx]) { errors = errors ++ &[1]std.builtin.TypeInfo.Error{field}; } } if (errors.len == 0) { curr_set = void; } else { curr_set = @Type(.{ .ErrorSet = errors }); } } inline for (std.meta.fields(SubSet)) |field| { if (@errorToInt(self.err) == field.value) return @errSetCast(SubSet, self.err); } return; } @compileError("Current set is empty"); } pub inline fn rest(self: Self, uniq: var) !void { if (curr_set == void) @compileError("Error set is empty"); return @errSetCast(curr_set, self.err); } }; } fn fieldStr(comptime set: type) []const u8 { comptime { var str: []const u8 = ""; for (std.meta.fields(set)) |f, idx| { if (idx != 0) str = str ++ ", "; str = str ++ f.name; } return str; } } const FooSet = error { AB, CD, EF, GH, }; fn foo() FooSet!void {} pub fn main() void { foo() catch |err| { var dispatcher = try ErrorDispatcher(.{}).init(err); dispatcher.case(error {AB, CD}) catch |sub_err| { // This will print AB, CD // @compileError(comptime fieldStr(@TypeOf(sub_err))); }; dispatcher.case(error{EF}) catch |sub_err| { // This will print EF // @compileError(comptime fieldStr(@TypeOf(sub_err))); }; dispatcher.rest(.{}) catch |sub_err| { // This will print GH // @compileError(comptime fieldStr(@TypeOf(sub_err))); }; //@compileLog(@TypeOf(dispatcher.rest(.{}))); dispatcher.case(error{GH}) catch |sub_err| { // This will print GH // @compileError(comptime fieldStr(@TypeOf(sub_err))); }; // This call will error with "Error set is empty" // try dispatcher.rest(.{}); }; }
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