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"); var rand: std.Random = undefined; const F1Error = enum { Account_Is_Empty, Investment_Lost, }; const F2Error = union(enum) { f1Error: F1Error }; const F3Error = union(enum) { f1Error: F1Error }; const F4Error = union(enum) { f2Error: F2Error, f3Error: F3Error, }; fn f1() F1Error { const n: u8 = rand.uintLessThan(u8, 9) + 1; if (n % 2 == 0) { return .Account_Is_Empty; } return .Investment_Lost; } fn f2() F2Error { return .{ .f1Error = f1() }; } fn f3() F3Error { return .{ .f1Error = f1() }; } fn f4() F4Error { const n: u8 = rand.uintLessThan(u8, 9) + 1; if (n % 2 == 0) { return .{ .f2Error = f2() }; } return .{ .f3Error = f3() }; } pub fn main() !void { var DefaultPrng = std.Random.DefaultPrng.init(@intCast(std.time.microTimestamp())); rand = DefaultPrng.random(); const err = f4(); switch (err) { F4Error.f2Error => |f2err| { switch (f2err) { F2Error.f1Error => |f1err| { switch (f1err) { .Account_Is_Empty => try std.io.getStdOut().writeAll("Aand it's gone\n"), else => try std.io.getStdOut().writeAll("This line is for bank members only\n"), } }, } }, F4Error.f3Error => |f3err| { switch (f3err) { F3Error.f1Error => |f1err| { switch (f1err) { .Investment_Lost => try std.io.getStdOut().writeAll("The money in your account didn't do well\n"), else => try std.io.getStdOut().writeAll("This line is for bank members only\n"), } }, } }, } try printTrace(std.io.getStdOut().writer(), err); try std.io.getStdOut().writeAll("\n\n"); } fn printTrace(writer: std.fs.File.Writer, trace: anytype) !void { const type_info = @typeInfo(@TypeOf(trace)); if (type_info == std.builtin.Type.@"enum") { var type_name = @typeName(@TypeOf(trace))[0..]; const last_dot_index = (std.mem.lastIndexOfScalar(u8, type_name, '.') orelse 0) + 1; try writer.writeAll(type_name[last_dot_index..]); try writer.writeAll("."); try writer.writeAll(@tagName(trace)); } else if (type_info.@"union".tag_type) |UnionTagType| { var type_name = @typeName(@TypeOf(trace))[0..]; const last_dot_index = (std.mem.lastIndexOfScalar(u8, type_name, '.') orelse 0) + 1; try writer.writeAll(type_name[last_dot_index..]); try writer.writeAll(" -> "); inline for (type_info.@"union".fields) |u_field| { if (trace == @field(UnionTagType, u_field.name)) { try printTrace(writer, @field(trace, u_field.name)); } } } }
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