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 { comptime var curr_set: type = undefined; return struct { const Self = @This(); err: anyerror, pub fn init(err: var) InitSet(@TypeOf(err), Self) { return .{ .err = err }; } pub fn case(self: *Self, comptime SubSet: type) RestrictSet(SubSet, SubSet!void) { inline for (std.meta.fields(SubSet)) |field| { if (@errorToInt(self.err) == field.value) return @errSetCast(SubSet, self.err); } } pub fn rest(self: Self, uniq: var) CheckSet(uniq) { // use the cached type, the actual var is empty return @errSetCast(CheckSet(uniq), self.err); } fn InitSet(comptime T: type, comptime Return: type) type { if (!std.meta.trait.is(.ErrorSet)(T)) @compileError("Value should be an error"); curr_set = T; return Return; } fn RestrictSet(comptime Removed: type, comptime Return: type) type { if (comptime !std.meta.trait.is(.ErrorSet)(Removed)) { @compileError("Sub set needs to be an error set..."); } if (@typeInfo(curr_set).ErrorSet) |CurrentSet| { var found_fields = [1]bool {false} ** CurrentSet.len; for (std.meta.fields(Removed)) |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 }); } } else { @compileError("Current set is empty"); } return Return; } fn CheckSet(uniq: var) type { if (curr_set == void) @compileError("Error set is empty"); return curr_set; } }; } 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 = ErrorDispatcher(.{}).init(err); dispatcher.case(error {AB, CD}) catch |sub_err| { // This will print AB, CD // @compileError(comptime fieldStr(@TypeOf(sub_err))); }; const narrow_err_1 = dispatcher.rest(.{}); // This will print EF GH //@compileError(comptime fieldStr(@TypeOf(dispatcher.rest(.{})))); dispatcher.case(error{EF}) catch |sub_err| { // This will print EF // @compileError(comptime fieldStr(@TypeOf(sub_err))); }; // This will print GH // @compileError(comptime fieldStr(@TypeOf(dispatcher.rest(.{})))); dispatcher.case(error{GH}) catch |sub_err| { // This will print GH // @compileError(comptime fieldStr(@TypeOf(sub_err))); }; // This will error out with // 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