Here are a couple things I wish zig fmt with pertaining to the idea that control-flow statements should be the first thing on a line:
// example 1
const user_index =
if (std.mem.eql(u8, user.discriminator, "0"))
user.id.timestamp % 6
else if (user.discriminator.len > 1) blk: {
const discrim = std.fmt.parseInt(u32, user.discriminator, 10) catch return error.InvalidDiscriminator;
break :blk discrim % 5;
} else {
std.log.err("invalid discriminator: '{s}'", .{user.discriminator});
return error.InvalidDiscriminator;
};
// example 2:
doAFunctionCallWithManyParams(param1, param2, param3, param4, param5)
catch |err| switch (err) {
error.Case1 => ...,
error.Case2 => ...,
};
Currently, these collapse to:
// example 1
const user_index = if (std.mem.eql(u8, user.discriminator, "0"))
user.id.timestamp % 6
else if (user.discriminator.len > 1) blk: {
const discrim = std.fmt.parseInt(u32, user.discriminator, 10) catch return error.InvalidDiscriminator;
break :blk discrim % 5;
} else {
std.log.err("invalid discriminator: '{s}'", .{user.discriminator});
return error.InvalidDiscriminator;
};
// example 2
doAFunctionCallWithManyParams(param1, param2, param3, param4, param5) catch |err| switch (err) {
error.Case1 => ...,
error.Case2 => ...,
};
One thing I really appreciated about go fmt is that it always forced control flow statements to be the first thing of each line. While I don’t necessarily agree that all control flow should be forced to the beginning of the line, I’d appreciate being able to at least allow this for many cases.
0 Comments
Log in to join the conversation.No comments yet. Be the first to share your thoughts.