Built-in to erase comptime dependencies

I have this idea of a builtin that allows us to erase comptime dependencies from functions. Something like this :

fn foo(a : anytime) @TypeOf(a) {...}

const foo_i32 = @bakeFn(foo, .{ i32 });

The second arg of @bakeFn is of type @FnComptimeDepTuple(foo).

What do you guys/gals think? Have something like this been brought up before?

You don’t need a builtin. Comptime factory pattern is all you need.

// untested code fragment
fn MakeFoo(comptime T: type) type {
    return struct {
        pub fn foo(arg: T) T { ... }

const foo_i32 = MakeFoo(i32).foo;
1 Like

Yeah, I know about the comptime factory pattern. But the idea of the builtin is that I don’t have do the comptime pattern for every function that I want to export.

If I don’t need the builtin, then here is a challenge : implement the builtin in zig userland. The interface of the builtin is it takes a fn and a tuple and returns a fn with the comptime dependencies baked in. The userland implementation should look like :-

fn makeFn(comptime f: anytype, comptime deps: anytype) ? { ? }

Good luck :grin:

You can group a whole family of functions parametrized by a generic type (or several types) into a single Factory function and generate concrete instantiations for them all at once.

Zig tries to be a minimalist language and number of builtins is very limited. You can try to submit a proposal for a language extension but Andrew’s attitude to new language proposals is justifiably hostile.

Welcome @omaraaa , it looks nice with one comptime parameter.
It gets really complex if you have more than one parameters and you want to set the value of just one of them.

1 Like

It can’t be done, since @typeInfo(f).Fn.return_type will be null when the return type is a function call.

Thanks @dimdin. Maybe I shouldn’t name it bakeFn because you are required to provide all of the parameters. Maybe the naming should be like @eraseComptime or something.

Yes, but sometimes* I get surprised at what can be done in userland in Zig.