I guess there is no easy way to do this? Without going by struct.
switch (a, b) {
(1, 2) => std.debug.print("1,2", .{}),
(2, 4) => std.debug.print("1,2", .{}),
else => std.debug.print("something else", .{}),
}
I guess there is no easy way to do this? Without going by struct.
switch (a, b) {
(1, 2) => std.debug.print("1,2", .{}),
(2, 4) => std.debug.print("1,2", .{}),
else => std.debug.print("something else", .{}),
}
Closest in 0.16 is probably something like:
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)
Which also brings up the point that tuples aka anonymous structs are not packed, otherwise you could do switch (.{a, b}). Should they be?
How would you specify the backing integer type? Switching on packed structs/unions is basically the same as switching on integers.
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.
Right. There’s a non-approved but open proposal on bit-packed tuples.
#12330? Hehe, I was just looking at that too.
Yep! Not a bad idea.
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:
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,
};
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.
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) => ...
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.
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.
That to me sounds exactly like what @ericlang actually wants:
I’d be down for a multi-pronged if personally as well!
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", .{}),
}
Tried this based on your post:
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
./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", .{}),
~~~~~~~^~~~~~~~~~
???
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.
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.
Because I do it the other way around.
/// 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));
}
};
@bitCast works both ways with packed structs. 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.
Something like this:
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});
}
all: 10102020208010102020208
black: 10102020208
white: 10102020208
white rooks: 2
The counts could also potentially be smaller integers (u4?) for extra memory savings.
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…