79194480

Date: 2024-11-16 03:50:09
Score: 0.5
Natty:
Report link

In this simple example, it would be better to create a single function that accepts a parameter of anytype, and the second parameter would use the builtin function @TypeOf to insure the two parameters are of the same type. I've modified your example below with the changes and updated the unit tests to be more idiomatic for Zig.

const std = @import("std");

fn defaultCompare(lhs: anytype, rhs: @TypeOf(lhs)) i8 {
    if (lhs > rhs) {
        return 1;
    } else if (lhs < rhs) {
        return -1;
    }
    return 0;
}

test "Fn" {
    try std.testing.expectEqual(defaultCompare(5, 16), -1);
    try std.testing.expectEqual(defaultCompare(6, 1), 1);
    try std.testing.expectEqual(defaultCompare(5, 5), 0);
}
Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • User mentioned (1): @TypeOf
  • Low reputation (0.5):
Posted by: Psycho Inferno