Is it possible to pass a function as a comptime parameter?

I’m back with another question: Can I pass in a function as a comptime parameter? I don’t mean a function pointer, just a function.

Can I do something equivalent to this?

pub fn myFunction() void {}

pub fn myComptimeFunction(comptime function: ???) void {
    // Call the function here:
    function();
}

pub fn main() void {
    myComptimeFunction(myFunction);
}

Is what I’m trying to do effectively the same as just passing the function as a function pointer? Or would there be a difference?

you can do function: fn () void.

Since this is a comptime only type, the comptime keyword is optional here.

Thanks! That’s just what I was looking for.

This is called a function body by the way.

Just ‘function’ is the function as defined, function body is the function passed by name at comptime, function pointer is the function address passed by pointer at runtime.

5 Likes