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

math: remove usingnamespace for mixins #1204

Open
slimsag opened this issue May 8, 2024 · 0 comments
Open

math: remove usingnamespace for mixins #1204

slimsag opened this issue May 8, 2024 · 0 comments
Labels
bug Something isn't working mach.math

Comments

@slimsag
Copy link
Member

slimsag commented May 8, 2024

suggested to me by mlugg', we could do either:

pub const Vec2 = struct {
    pub fn init(xs: u32, ys: u32) Vec2 {
        return .{ .v = .{ xs, ys } };
    }
    const Shared = SharedImpl(@This());
    pub const add = Shared.add;
    pub const sub = Shared.sub;
};

pub const Vec3 = struct {
    pub fn init(xs: u32, ys: u32, zs: u32) Vec2 {
        return .{ .v = .{ xs, ys, zs } };
    }
    const Shared = SharedImpl(@This());
    pub const add = Shared.add;
    pub const sub = Shared.sub;
};

fn SharedImpl(comptime T: type) type {
    return struct {
        fn add(a: *const T, b: *const T) T {
            return .{ .v = a.v + b.v };
        }
        fn sub(a: *const T, b: *const T) T {
            return .{ .v = a.v - b.v };
        }
    };
}

or new mixin strategy for more conventional mixin strategies:

pub const Vec2 = struct {
    v: @Vector(2, u32),
    shared: Shared(@This()),
    pub fn init(xs: u32, ys: u32) Vec2 {
        return .{ .v = .{ xs, ys } };
    }
};

pub const Vec3 = struct {
    v: @Vector(3, u32),
    shared: Shared(@This()),
    pub fn init(xs: u32, ys: u32, zs: u32) Vec2 {
        return .{ .v = .{ xs, ys, zs } };
    }
};

fn Shared(comptime T: type) type {
    return struct {
        fn add(a_shared: *const @This(), b: *const T) T {
            const a: *T = @fieldParentPtr("shared", a_shared);
            return .{ .v = a.v + b.v };
        }
        fn sub(a_shared: *const @This(), b: *const T) T {
            const a: *T = @fieldParentPtr("shared", a_shared);
            return .{ .v = a.v - b.v };
        }
    };
}
@slimsag slimsag added bug Something isn't working mach.math labels May 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working mach.math
Projects
None yet
Development

No branches or pull requests

1 participant