#Let there be anytype (ArrayList)

1 messages · Page 1 of 1 (latest)

deep lynx
#
pub const onCastFn = ?*const fn (self: *Spell, entity: *Entity) void;

pub const Spell = struct {
    name: []const u8,
    onCast: onCastFn,
    isActive: bool,

    pub fn init(name: []const u8, onCast: onCastFn) @This() {
        return @This(){
            .name = name,
            .onCast = onCast,
            .isActive = false,
        };
    }

    pub fn cast(self: @This(), args: anytype) void {
        if (self.onCast) |onCast| {
            onCast(args[0], args[1]);
        }
    }
};

pub const SpellBook = struct {
    spells: std.ArrayList(Spell),
...

Is it possible / workaround for me to use anytime for my onCastFn?
I cant use anytype because the ArrayList requires that the parameter to be known in comptime

note: argument to function being called at comptime must be comptime-known
deft cosmos
#

I don't think so, but if you just want to accept different types, you can use an union

half arch
#

anytype is essentially syntax sugar for comptime T: type, param: T