# Switch on two values?

**URL:** <https://ziggit.dev/t/switch-on-two-values/15063>\
**Category:** Help\
**Tags:** language\
**Created:** [April 20, 2026, 12:08pm UTC](https://ziggit.dev/t/switch-on-two-values/15063 "2026-04-20T12:08:32Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![ericlang](https://ziggit.dev/user_avatar/ziggit.dev/ericlang/32/8515_2.png) [@ericlang](https://ziggit.dev/u/ericlang)\
**Post date:** [April 20, 2026, 12:08pm UTC](https://ziggit.dev/t/switch-on-two-values/15063/1 "2026-04-20T12:08:32Z")

</div>

I guess there is no easy way to do this? Without going by struct.

```zig
switch (a, b) {
    (1, 2) => std.debug.print("1,2", .{}),
    (2, 4) => std.debug.print("1,2", .{}),
    else => std.debug.print("something else", .{}),
}

```

---

<div class="post-metadata">

**Author:** ![cryptocode](https://ziggit.dev/user_avatar/ziggit.dev/cryptocode/32/817_2.png) [@cryptocode](https://ziggit.dev/u/cryptocode)\
**Post date:** [April 20, 2026, 12:16pm UTC](https://ziggit.dev/t/switch-on-two-values/15063/2 "2026-04-20T12:16:15Z")

</div>

Closest in 0.16 is probably something like:

```zig
    const Pair = packed struct(u64) {
        a: u32,
        b: u32,
    };

    const x: u32 = 9;
    const y: u32 = 6;

    switch (Pair{ .a = x, .b = y }) {
        .{ .a = 6, .b = 9 } => std.debug.print("6/9\n", .{}),
        .{ .a = 9, .b = 6 } => std.debug.print("9/6\n", .{}),
        else => std.debug.print("Something else\n", .{}),
    }

```

(since packed structs can be prongs)

---

<div class="post-metadata">

**Author:** ![Kass](https://ziggit.dev/user_avatar/ziggit.dev/kass/32/6557_2.png) [@Kass](https://ziggit.dev/u/Kass)\
**Post date:** [April 20, 2026, 12:18pm UTC](https://ziggit.dev/t/switch-on-two-values/15063/3 "2026-04-20T12:18:14Z")

</div>

Which also brings up the point that tuples aka anonymous structs are not packed, otherwise you could do `switch (.{a, b})`. Should they be?

---

<div class="post-metadata">

**Author:** ![cryptocode](https://ziggit.dev/user_avatar/ziggit.dev/cryptocode/32/817_2.png) [@cryptocode](https://ziggit.dev/u/cryptocode)\
**Post date:** [April 20, 2026, 12:20pm UTC](https://ziggit.dev/t/switch-on-two-values/15063/4 "2026-04-20T12:20:41Z")

</div>

How would you specify the backing integer type? Switching on packed structs/unions is basically the same as switching on integers.

---

<div class="post-metadata">

**Author:** ![Kass](https://ziggit.dev/user_avatar/ziggit.dev/kass/32/6557_2.png) [@Kass](https://ziggit.dev/u/Kass)\
**Post date:** [April 20, 2026, 12:24pm UTC](https://ziggit.dev/t/switch-on-two-values/15063/5 "2026-04-20T12:24:03Z")

</div>

I mean, I was thinking same way as deciding on what to name it – you wouldn’t. Just let the compiler handle it, like if you omit the `(u64)` from `packed struct(u64)` in your example.

---

<div class="post-metadata">

**Author:** ![cryptocode](https://ziggit.dev/user_avatar/ziggit.dev/cryptocode/32/817_2.png) [@cryptocode](https://ziggit.dev/u/cryptocode)\
**Post date:** [April 20, 2026, 12:27pm UTC](https://ziggit.dev/t/switch-on-two-values/15063/6 "2026-04-20T12:27:50Z")

</div>

Right. There’s a non-approved but open proposal on bit-packed tuples.

---

<div class="post-metadata">

**Author:** ![Kass](https://ziggit.dev/user_avatar/ziggit.dev/kass/32/6557_2.png) [@Kass](https://ziggit.dev/u/Kass)\
**Post date:** [April 20, 2026, 12:29pm UTC](https://ziggit.dev/t/switch-on-two-values/15063/7 "2026-04-20T12:29:22Z")

</div>

[#12330](https://github.com/ziglang/zig/issues/12330)? Hehe, I was just looking at that too.

---

<div class="post-metadata">

**Author:** ![cryptocode](https://ziggit.dev/user_avatar/ziggit.dev/cryptocode/32/817_2.png) [@cryptocode](https://ziggit.dev/u/cryptocode)\
**Post date:** [April 20, 2026, 12:31pm UTC](https://ziggit.dev/t/switch-on-two-values/15063/8 "2026-04-20T12:31:54Z")

</div>

Yep! Not a bad idea.

---

<div class="post-metadata">

**Author:** ![ericlang](https://ziggit.dev/user_avatar/ziggit.dev/ericlang/32/8515_2.png) [@ericlang](https://ziggit.dev/u/ericlang)\
**Post date:** [April 20, 2026, 12:32pm UTC](https://ziggit.dev/t/switch-on-two-values/15063/9 "2026-04-20T12:32:12Z")

</div>

yeah packed struct is possible. but i need raw values.  
I solved it with a inline function to split u96 into 2 x u48 and vice versa.  
something like:

```zig
    const m: u96 = pos.material.decode_both(winner, loser);

    return switch (m) {
        Material.encode_96(KR, KB) => scales.drawish,
        Material.encode_96(KR, KN) => scales.drawish,
        Material.encode_96(KBB, KB) => scales.drawish,
        Material.encode_96(KBB, KN) => scales.winning,
        Material.encode_96(KRR, KB) => scales.winning,
        else => null,
    };

```

---

<div class="post-metadata">

**Author:** ![invlpg](https://ziggit.dev/letter_avatar_proxy/v4/letter/i/13edae/32.png) [@invlpg](https://ziggit.dev/u/invlpg)\
**Post date:** [April 20, 2026, 12:52pm UTC](https://ziggit.dev/t/switch-on-two-values/15063/10 "2026-04-20T12:52:45Z")

</div>

May be cleaner to use an enum instead of a u96. Then you can use .decl literals in the switch cases and avoid repeating namespace/type names.

```zig
const EncodedPair = enum(u96) {
    _,
    inline fn init(a: u48, b: u48) @This() {
        return @enumFromInt((@as(u96, a) << 48) | b);
    }
};

switch (EncodedPair.init(winner, loser)) {
    .init(KR, KB) => ...

```

---

<div class="post-metadata">

**Author:** ![spiffyk](https://ziggit.dev/user_avatar/ziggit.dev/spiffyk/32/7202_2.png) [@spiffyk](https://ziggit.dev/u/spiffyk)\
**Post date:** [April 20, 2026, 12:57pm UTC](https://ziggit.dev/t/switch-on-two-values/15063/11 "2026-04-20T12:57:05Z")

</div>

Am I missing something? Why not use a `packed struct(u96)` and `@bitCast` between it and the raw integer value? The conversion is fully defined.

---

<div class="post-metadata">

**Author:** ![invlpg](https://ziggit.dev/letter_avatar_proxy/v4/letter/i/13edae/32.png) [@invlpg](https://ziggit.dev/u/invlpg)\
**Post date:** [April 20, 2026, 1:03pm UTC](https://ziggit.dev/t/switch-on-two-values/15063/12 "2026-04-20T13:03:13Z")

</div>

No, it could also be implemented that way. I just tend to go for enums when I need to wrap an integer with something strongly typed. I typically save packed structs for either dealing with hardware or when benchmarks suggest that densely packing will help.

It should come out more or less the same though after optimizations here. Semantically, enum to me means: the underlying value can be treated as somewhat opaque; while a packed struct says that we really care about the layout and might want access to the underlying fields.

---

<div class="post-metadata">

**Author:** ![spiffyk](https://ziggit.dev/user_avatar/ziggit.dev/spiffyk/32/7202_2.png) [@spiffyk](https://ziggit.dev/u/spiffyk)\
**Post date:** [April 20, 2026, 1:07pm UTC](https://ziggit.dev/t/switch-on-two-values/15063/13 "2026-04-20T13:07:30Z")

</div>

> [@invlpg](#):
>
> a packed struct says that we really care about the layout and might want access to the underlying fields

That to me sounds exactly like what @ericlang actually wants:

> [@ericlang](#):
>
> but i need raw values.  
> I solved it with a inline function to split u96 into 2 x u48 and vice versa.

---

<div class="post-metadata">

**Author:** ![matklad](https://ziggit.dev/user_avatar/ziggit.dev/matklad/32/2462_2.png) [@matklad](https://ziggit.dev/u/matklad)\
**Post date:** [April 20, 2026, 1:24pm UTC](https://ziggit.dev/t/switch-on-two-values/15063/14 "2026-04-20T13:24:22Z")

</div>

I’d be down for a multi-pronged if personally as well!

```zig
    if {
        a == 1 and b == 2 => std.debug.print("1,2", .{}),
        a == 2 and b == 4 => std.debug.print("2,4", .{}),
        else => std.debug.print("something else", .{}),
    }

```

---

<div class="post-metadata">

**Author:** ![Kass](https://ziggit.dev/user_avatar/ziggit.dev/kass/32/6557_2.png) [@Kass](https://ziggit.dev/u/Kass)\
**Post date:** [April 20, 2026, 1:33pm UTC](https://ziggit.dev/t/switch-on-two-values/15063/15 "2026-04-20T13:33:07Z")

</div>

Tried this based on your post:

```zig
pub fn main() void {
    const x: u32 = 2;
    const y: u32 = 1;

    switch (true) {
        x == 1 and y == 2 => std.debug.print("1,2", .{}),
        x == 2 and y == 4 => std.debug.print("2,4", .{}),
        else => std.debug.print("something else", .{}),
    }
}

const std = @import("std");

```

And the compiler bailed with

```zig
./example.zig:7:16: error: duplicate switch value
        x == 2 and y == 4 => std.debug.print("2,4", .{}),
        ~~~~~~~^~~~~~~~~~./example.zig:6:16: note: previous value here
        x == 1 and y == 2 => std.debug.print("1,2", .{}),
        ~~~~~~~^~~~~~~~~~

```

???

[https://codeberg.org/ziglang/zig/src/commit/4e2147d14e34e6891f2a8f2e2c7f28fdb6d92c2a/src/Sema.zig#L12673](https://codeberg.org/ziglang/zig/src/commit/4e2147d14e34e6891f2a8f2e2c7f28fdb6d92c2a/src/Sema.zig#L12673)

I guess `and` expressions don’t resolve to `.bool` in Sema? But why?

Edit: oh, duh. Because it’s comptime and both prongs are `false`.

---

<div class="post-metadata">

**Author:** ![spiffyk](https://ziggit.dev/user_avatar/ziggit.dev/spiffyk/32/7202_2.png) [@spiffyk](https://ziggit.dev/u/spiffyk)\
**Post date:** [April 20, 2026, 1:42pm UTC](https://ziggit.dev/t/switch-on-two-values/15063/16 "2026-04-20T13:42:57Z")

</div>

> [@Kass](#):
>
> Edit: oh, duh. Because it’s comptime and both prongs are `false`.

Just wanted to reply with that. On top of it, if `x` and `y` were runtime-known, it wouldn’t compile because switch prongs’ left hand sides need to be comptime-known.

---

<div class="post-metadata">

**Author:** ![ericlang](https://ziggit.dev/user_avatar/ziggit.dev/ericlang/32/8515_2.png) [@ericlang](https://ziggit.dev/u/ericlang)\
**Post date:** [April 20, 2026, 1:44pm UTC](https://ziggit.dev/t/switch-on-two-values/15063/17 "2026-04-20T13:44:12Z")

</div>

Because I do it the other way around.

```zig
/// Piececounts. King is always 1.
pub const Material = struct {
    /// Indexing by [color][piecetype].
    counts: [2][6]u8,

    pub const empty: Material = .{
        .counts = .{
            .{ 0, 0, 0, 0, 0, 0 },
            .{ 0, 0, 0, 0, 0, 0 },
        }
    };

    pub const default: Material = .{
        .counts = .{
            .{ 8, 2, 2, 2, 1, 1 },
            .{ 8, 2, 2, 2, 1, 1 },
        }
    };

    /// Comptime only to construct endgame consts.
    pub inline fn encode_48(p: u8, n: u8, b: u8, r: u8, q: u8) u48 {
        return @as(u48, p) | @as(u48, n) << 8 | @as(u48, b) << 16 | @as(u48, r) << 24 | @as(u48, q) << 32 | @as(u48, 1) << 40;
    }

    /// Comptime only to construct endgame consts.
    pub inline fn encode_96(w: u48, b: u48) u96 {
        return @as(u96, w) | @as(u96, b) << 48;
    }

    pub fn decode(self: Material) u96 {
        return @bitCast(self.counts);
    }

    pub fn decode_side(self: Material, us: Color) u48 {
        return @bitCast(self.counts[us.u]);
    }

    pub fn decode_both(self: Material, winner: Color, loser: Color) u96 {
        return encode_96(self.decode_side(winner), self.decode_side(loser));
    }
};

```

---

<div class="post-metadata">

**Author:** ![spiffyk](https://ziggit.dev/user_avatar/ziggit.dev/spiffyk/32/7202_2.png) [@spiffyk](https://ziggit.dev/u/spiffyk)\
**Post date:** [April 20, 2026, 1:53pm UTC](https://ziggit.dev/t/switch-on-two-values/15063/18 "2026-04-20T13:53:29Z")

</div>

`@bitCast` works both ways with `packed struct`s. You can `@bitCast` from `packed struct(u96)` to `u96` and vice versa. Plus, if you really wanted, you could also use a `packed union` to express all of that.

---

<div class="post-metadata">

**Author:** ![spiffyk](https://ziggit.dev/user_avatar/ziggit.dev/spiffyk/32/7202_2.png) [@spiffyk](https://ziggit.dev/u/spiffyk)\
**Post date:** [April 20, 2026, 2:27pm UTC](https://ziggit.dev/t/switch-on-two-values/15063/19 "2026-04-20T14:27:10Z")

</div>

Something like this:

```zig
pub const Material = packed struct(u96) {
    white: Side,
    black: Side,

    pub const Side = packed struct(u48) {
        pawn: u8,
        knight: u8,
        bishop: u8,
        rook: u8,
        queen: u8,
        king: u8,

        pub const empty: Side = @bitCast(0);
        pub const default: Side = .{
            .pawn = 8,
            .knight = 2,
            .bishop = 2,
            .rook = 2,
            .queen = 1,
            .king = 1,
        }; // or `@bitCast(0x010102020208)` if you feel spicy
    };

    pub const empty: Material = @bitCast(0);
    pub const default: Material = .{
        .black = .default,
        .white = .default,
    };
};

pub fn main() void {
    const std = @import("std");
    const m: Material = .default;
    std.debug.print("all: {x}\n", .{@as(u96, @bitCast(m))});
    std.debug.print("black: {x}\n", .{@as(u48, @bitCast(m.black))});
    std.debug.print("white: {x}\n", .{@as(u48, @bitCast(m.white))});
    std.debug.print("white rooks: {x}\n", .{m.white.rook});
}

```

```plaintext
all: 10102020208010102020208
black: 10102020208
white: 10102020208
white rooks: 2

```

The counts could also potentially be smaller integers (`u4`?) for extra memory savings.

---

<div class="post-metadata">

**Author:** ![ericlang](https://ziggit.dev/user_avatar/ziggit.dev/ericlang/32/8515_2.png) [@ericlang](https://ziggit.dev/u/ericlang)\
**Post date:** [April 20, 2026, 3:02pm UTC](https://ziggit.dev/t/switch-on-two-values/15063/20 "2026-04-20T15:02:15Z")

</div>

ok i use packed unions in a lot of cases.  
But this one contains an array, so not allowed to pack.  
It is a “cornercase” anyway…
