Mixin alternative using const declarations

Just to note, you can extend this quite easily using comptime function parameters that take in your meta-functions to be specialized. The Bar example is highly trivial but it matches our use case for Fluent - not true in general but just for demonstration’s sake.

A little more involved would be something like so… (pseudo-code):

pub fn MyImpl(comptime Self: type) type {
    return struct {
        pub fn foo(self: Self, ...) ...
        pub fn bar(self: Self, ...) ...
    };
}

pub fn Thing(comptime T: type, comptime Impl: anytype) type {
    return struct {
        const Self = @This();     
        const foo = Impl(Self).foo;
        const bar = Impl(Self).bar;
    };
}

const Widget = Thing(u8, MyImpl);

The major difference is there’s no “silent-injection” as it were. You have to specifically declare foo, bar, etc... in the interface.

6 Likes