Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch remove else clause #91

Merged
merged 7 commits into from
Feb 2, 2025
Merged

Conversation

42LM
Copy link
Contributor

@42LM 42LM commented Jan 31, 2025

Hey there,
this PR exchanges else prongs with explicit types using exhaustive option checks when possible.

It only exchanges where it seemed reasonable to me. So there is still room for more exchanges here like e.g.:

fn append_varint(pb: *ArrayList(u8), value: anytype, comptime varint_type: VarintType) Allocator.Error!void {
    switch (@typeInfo(@TypeOf(value))) {
        .Enum => try append_as_varint(pb, @as(i32, @intFromEnum(value)), varint_type),
        .Bool => try append_as_varint(pb, @as(u8, if (value) 1 else 0), varint_type),
        else => try append_as_varint(pb, value, varint_type),
    }
}

Since the std.builtin.Type is a pretty large union the list gets pretty big and i did not like it:

fn append_varint(pb: *ArrayList(u8), value: anytype, comptime varint_type: VarintType) Allocator.Error!void {
    switch (@typeInfo(@TypeOf(value))) {
        .Enum => try append_as_varint(pb, @as(i32, @intFromEnum(value)), varint_type),
        .Bool => try append_as_varint(pb, @as(u8, if (value) 1 else 0), varint_type),
        .Type, .NoReturn, .Int, .Float, .Pointer, .Array, .Struct, .ComptimeFloat, .ComptimeInit, .Undefined, .Null, .Optional, .ErrorUnion, .ErrorSet, .Union, .Fn, .Opaque, .Frame, .AnyFramge, .Vector, .EnumLiteral => try append_as_varint(pb, value, varint_type),
    }
}

While i am more okey with this last one, it also consumes quite an amount of lines plus zls always reformats it to the above example ;)

fn append_varint(pb: *ArrayList(u8), value: anytype, comptime varint_type: VarintType) Allocator.Error!void {
    switch (@typeInfo(@TypeOf(value))) {
        .Enum => try append_as_varint(pb, @as(i32, @intFromEnum(value)), varint_type),
        .Bool => try append_as_varint(pb, @as(u8, if (value) 1 else 0), varint_type),
        .Type,
        .NoReturn, 
        .Int, 
        .Float, 
        .Pointer, 
        .Array,
        .Struct, 
        .ComptimeFloat,
        .ComptimeInit, 
        .Undefined, 
        .Null, 
        .Optional, 
        .ErrorUnion, 
        .ErrorSet, 
        .Union, 
        .Fn, 
        .Opaque, 
        .Frame, 
        .AnyFramge, 
        .Vector, 
        .EnumLiteral => try append_as_varint(pb, value, varint_type),
    }
}

Is there something one could do about this? My zig knowledge comes to an end here, in my dreams i would somehow put those values in a const and than use that in the switch. However i was not successful with this.

WDYT? Should the else prong stay in the case of a huge list?

resolves #78

@Arwalk Arwalk self-requested a review February 1, 2025 21:39
Copy link
Owner

@Arwalk Arwalk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't found any other else clause that could be expanded, so this looks OK.
That said, there is a slight change that can be made for the append_varint function.

diff --git a/src/protobuf.zig b/src/protobuf.zig
index 27e5ad6..89168c5 100644
--- a/src/protobuf.zig
+++ b/src/protobuf.zig
@@ -249,7 +249,8 @@ fn append_varint(pb: *ArrayList(u8), value: anytype, comptime varint_type: Varin
     switch (@typeInfo(@TypeOf(value))) {
         .Enum => try append_as_varint(pb, @as(i32, @intFromEnum(value)), varint_type),
         .Bool => try append_as_varint(pb, @as(u8, if (value) 1 else 0), varint_type),
-        else => try append_as_varint(pb, value, varint_type),
+        .Int => try append_as_varint(pb, value, varint_type),
+        else => @compileError("Should not pass a value of type " ++ @typeInfo(@TypeOf(value)) ++ "here")
     }
 }

This should be a better approach for this case than just putting the else method.
Otherwise, it's nice. Thanks.

@42LM 42LM requested a review from Arwalk February 2, 2025 10:07
@Arwalk Arwalk merged commit 8b47dc3 into Arwalk:master Feb 2, 2025
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Remove all else clauses when possible
2 participants