I want to modify or compute a value before assigning it to a const. Is there a way to do this in a single line expression?
I can use labeled block syntax, but I’m wondering if there’s another syntax that would be easier to read.
const std = @import("std");
const print = std.debug.print;
fn f(x: i32) i32 {
return x + 1;
}
pub fn main() void {
{
const x, const y = blk: {
const a = f(2);
break :blk .{ a * a, a + a };
};
print("{d}, {d}\n", .{ x, y });
}
{
// Is there a way to do something like this
// const x, const y = f(2) |a| .{ a * a, a + a };
// print("{d}, {d}\n", .{ x, y });
// Not like this
const x, const y = if (@as(?i32, f(2))) |a| .{ a * a, a + a } else unreachable;
print("{d}, {d}\n", .{ x, y });
}
{
// Is there a way to do something like this
// f(2) |a| .{ a * a, a + a } |x,y| print("{d}, {d}\n", .{ x, y });
// Not like this
if (@as(?struct { i32, i32 }, if (@as(?i32, f(2))) |a| .{ a * a, a + a } else unreachable)) |a| print("{d}, {d}", .{ a.@"0", a.@"1" }) else unreachable;
}
}