#Inline functions?

1 messages · Page 1 of 1 (latest)

somber hull
#

I have a switch function with many cases, like so:

        0x1a => {
            return .{
                .label = "LD A,(DE)",
                .length = 1,
                .cycles = 8,
                .step = ldADE,
            };
        },

The step functions are pretty light weight, sometimes more complex/wrapping others, but in many cases are one liners like so:

fn ldADE(cpu: *c.SM83) void {
    cpu.registers.af.setHi(cpu.memory.read(cpu.registers.de.hilo()));
}

I'd love to define these in-line within the switch, so there's less indirection. Could I do that in Zig?

coarse reef
somber hull
#

Oh that's nice

coarse reef
#

and to answer your question:

switch (foo) {
    0x1a => return .{
        .label = "LD A,(DE)",
        .length = 1,
        .cycles = 8,
        .step = struct {
            fn ldADE(cpu: *c.SM83) void {
                cpu.registers.af.setHi(cpu.memory.read(cpu.registers.de.hilo()));
            }
        }.ldADE,
    },
}
#

there is no way to define a function "inline" without wrapping it in a struct like so