30 lines
746 B
Zig
30 lines
746 B
Zig
const std = @import("std");
|
|
|
|
pub fn Box(comptime T: type) type {
|
|
return struct {
|
|
val: T,
|
|
};
|
|
}
|
|
|
|
pub fn AddBoxType(comptime LHS: type, comptime RHS: type) type {
|
|
const x = std.mem.zeroes(std.meta.FieldType(LHS, .val));
|
|
const y = std.mem.zeroes(std.meta.FieldType(RHS, .val));
|
|
return Box(@TypeOf(x + y));
|
|
}
|
|
|
|
pub fn addbox(lhs: anytype, rhs: anytype) AddBoxType(@TypeOf(lhs), @TypeOf(rhs)) {
|
|
return .{ .val = lhs.val + rhs.val };
|
|
}
|
|
|
|
test {
|
|
std.testing.refAllDecls(@This());
|
|
}
|
|
|
|
test "widen" {
|
|
const foo: Box(u8) = .{ .val = 99 };
|
|
const bar: Box(u16) = .{ .val = 599 };
|
|
const actual = addbox(foo, bar);
|
|
const expected: Box(u16) = .{ .val = 698 };
|
|
try std.testing.expectEqual(expected, actual);
|
|
}
|