www.ziglang.org icon indicating copy to clipboard operation
www.ziglang.org copied to clipboard

Not mentioned in docs: `else => |x|` in switch

Open ghost opened this issue 4 years ago • 1 comments

This feature seems to exist but it's not mentioned in the docs.

else supports a capture group which probably just returns the value that was in the switch(...). parentheses.

const std = @import("std");
pub fn main() void {
    var b: i32 = 5;
    switch (b) {
        else => |x| std.debug.warn("{}\n", .{x}), // prints 5
    }
}

ghost avatar May 01 '20 16:05 ghost

the capture is supported on all arms of a switch to get the value that was passed in

nektro avatar Sep 17 '21 22:09 nektro