Skip to content

storing an interface in a generic type, like ArrayList? #3

Closed

Description

@emperor-limitless

so I was planning to use this for a stack based state machine thing, which requires me to have an ArrrayList or the equivilant, however, if I do

const std = @import("std");
const interface = @import("interface").Interface;

const State = interface(.{
    .onEnter = fn(anytype),
    // ...
});

const StateManager = struct {
    stack: ArrayList(anytype), // error.
}

so I'm wondering how can I store instances of struct? Note, it's not limited to one implementation, I could have a MenuState, A Gameplay state, etc, so having it store anything that implements the State interface is necessary

Activity

  1. nilslice commented on Nov 26, 2024

    @nilslice
    Owner

    Hey @emperor-limitless -

    I am planning to re-implement this module such that it would enable use cases like yours. It's currently more of an experiment in designing interface validation, but as you encountered it does not work with generic collections.

    In order to do this, I will need to instead generate vtable style interfaces like std.mem.Allocator from the Interface(.{ ... }) definition.

    Sorry it doesn't meet your current needs, but I think handling your use case in the near future is absolutely possible and what I want this module to do.

  2. Xion commented on Jan 22, 2025

    @Xion

    Sorry it doesn't meet your current needs, but I think handling your use case in the near future is absolutely possible and what I want this module to do.

    I'm really curious to see how an automatic generation of vtables and their impls could be accomplished at comptime. From my limited digging into the issue, I concluded this isn't really possible without outside code generation due to the fact that reified types cannot have decls and never will.

  3. nilslice commented on Oct 19, 2025

    @nilslice
    Owner

    @emperor-limitless @Xion, I have a PR up that adds vtable implemented interfaces. It's pretty close to what I was hoping to reach. Let me know if you have a chance to take a look: #9

  4. nilslice commented on Oct 19, 2025

    @nilslice
    Owner

    additionally, @emperor-limitless, I have a test that demonstrates the collection limitation you brought up: https://github.com/nilslice/zig-interface/blob/updated-interface/test/collections.zig, which should now be resolved.

  5. nilslice commented on Oct 28, 2025

    @nilslice
    Owner

    I've merged #9 which now will let you specify the type of your container as the Interface type you define

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions