Combine mixin and template parameter

I am having trouble combining a mixin with a template parameter. Separately I get both mixins and template parameters to work correctly.

The partial mixin

pub fn StringMixin(comptime Self: type) type {
    return struct {
        pub fn addString(self: *Self, source: []const u8) !void {
            try self.growExtra(source.len);

Working HeapString that only uses the StringMixin

pub const HeapString = struct {
    const Self = @This();
    m_buffer: []u8 = undefined,
    m_bufferSize: usize = 0,
    m_bufferLen: usize = 0,

    pub usingnamespace StringMixin(HeapString);

    pub fn init(initialSize: usize) !HeapString {

Non working StackString that combines StringMixin with template parameter

pub fn StackString(MAX_SIZE: comptime_int) type {
    return struct {
        const Self = @This();
        pub usingnamespace StringMixin(StackString(MAX_SIZE));

        m_buffer: [MAX_SIZE+1] u8 = undefined,
        m_bufferSize: usize = MAX_SIZE,
        m_bufferLen: usize = 0,

Do you know of an applicable example. Depending on the various combinations I try, I get different errors with the common theme being that I cannot instantiate the type without compilation errors.

Thanks to Iggy’s Mixins in Zig :: Dr Iggy's Coding Adventures that at least got me to here.

Apologies… it is working like a charm, late in the day, missing things on the screen:

    var ss:  utils.string.StackString(255) = .{};
    try ss.addString("Heelo");
    try ss.addString(" World");
    print("{s}", .{ss.toString()});

Nothing wrong with the code in other words, it compiles and gives the anticipated result in other words.

FYI, usingnamespace has been removed in the upcoming release:

1 Like

Hi @squeek502 I saw hints about that… is it removed completely or replaced by a different keyword, for if it is removed completely, then I am obviously barking up the wrong tree wrt to design? Will there by a like substitute that will not force me to rewrite “everything”, or is the pattern terminated with the next release?

As per my understanding, the idea was not to remove the concept of mixins but replace the keyboard/name and maybe some behaviour to it, but essentially still retain mixins.

Removed without replacement, see here for the reasoning:

Thanks @squeek502 , I have changed the classes accordingly. Thanks for your response as, if I went down this route I would have had potentially much code to change in future.

The suggested way is a little bit different to a mixin (kind of feels more like general composition - and yes I know, technically mixins are principally composition e.g. Golang).

Changing the code was easy… don’t like the extra “named” dependency, neither by its definition nor by its effect on code, but that is not the end of the world either.

I do however appreciate the issues raised by the thread posted… and the redundancy of using namespace… And in all honesty, using namespace for a mixin is as natural as binge eating to a diet.

1 Like