#Return a function from comptime

1 messages · Page 1 of 1 (latest)

steep haven
#

do you mean something like this?

fn GenGeneric(comptime T: type) fn (T) void {
    return struct {
        fn generic(value: T) void {
            // ...
        }
    }.generic;
}
#

that would require the use of some kind of macro, which doesn't exist in zig

#

you could pass a function pas a parameter

#

but then that's kind of, well, redundant

#

what are you trying to achieve with this?

#

is this in some way an attempt at a bridge between the functional paradigm and the procedural?

#

well, a word of warning: zig is, in design, very strictly procedural, with very sparse functional elements

#

trying to use functional approaches doesn't tend to look or work as well as just using the procedural approach in zig, which is what it is designed around

slow estuary
#

I will also point out that TYPE { STATEMENTS; } is invalid syntax anyway.
TYPE { ... } is a structure literal, and cannot contain statements.

#

std.ArrayList(u8), as an example, is just calling std.ArrayList (a function) and returning a type, which can then be used as std.ArrayList(u8) { ... fields ... } if you wanted to.