Function compare [src]

This function does the same thing as comparison operators, however the operator is a runtime-known enum value. Works on any operands that support comparison operators.

Prototype

pub fn compare(a: anytype, op: CompareOperator, b: anytype) bool

Parameters

op: CompareOperator

Example

test compare { try testing.expect(compare(@as(i8, -1), .lt, @as(u8, 255))); try testing.expect(compare(@as(i8, 2), .gt, @as(u8, 1))); try testing.expect(!compare(@as(i8, -1), .gte, @as(u8, 255))); try testing.expect(compare(@as(u8, 255), .gt, @as(i8, -1))); try testing.expect(!compare(@as(u8, 255), .lte, @as(i8, -1))); try testing.expect(compare(@as(i8, -1), .lt, @as(u9, 255))); try testing.expect(!compare(@as(i8, -1), .gte, @as(u9, 255))); try testing.expect(compare(@as(u9, 255), .gt, @as(i8, -1))); try testing.expect(!compare(@as(u9, 255), .lte, @as(i8, -1))); try testing.expect(compare(@as(i9, -1), .lt, @as(u8, 255))); try testing.expect(!compare(@as(i9, -1), .gte, @as(u8, 255))); try testing.expect(compare(@as(u8, 255), .gt, @as(i9, -1))); try testing.expect(!compare(@as(u8, 255), .lte, @as(i9, -1))); try testing.expect(compare(@as(u8, 1), .lt, @as(u8, 2))); try testing.expect(@as(u8, @bitCast(@as(i8, -1))) == @as(u8, 255)); try testing.expect(!compare(@as(u8, 255), .eq, @as(i8, -1))); try testing.expect(compare(@as(u8, 1), .eq, @as(u8, 1))); }

Source

pub fn compare(a: anytype, op: CompareOperator, b: anytype) bool { return switch (op) { .lt => a < b, .lte => a <= b, .eq => a == b, .neq => a != b, .gt => a > b, .gte => a >= b, }; }