Skip to content

ability to mark functions as wanting to be comptime if possible #425

@andrewrk

Description

@andrewrk

Use case: if the parameter to assert is comptime known, we for sure want to execute the assert function call at compile-time. It would catch a lot of bugs at compile-time instead of generating, essentially assert(false) in some places.

So here's a proposal to have a way to mark a function as "eager to be comptime", which means that if all parameters are comptime known, the compiler executes the function and uses the result instead of emitting a runtime function call.

We can't have all functions do this, because functions can contain runtime side effects even if all params are comptime known, and we do want to have compile errors for hitting runtime side effects at compile time for these kind of functions. Case in point, we want the if (!ok) unreachable to be a compile-error in the assert function if ok is false. So this is why the user has to mark a function as "eager to be run at compile time".

Here's one syntax proposal:

comptime fn assert(ok: bool) {
    if (!ok) unreachable;
}

The thing I don't like about this is it looks like assert always has to be run at compile-time, but it doesn't. It also looks like you have to mark a function comptime before you can use it at compile-time, and that's also not true.

Metadata

Metadata

Assignees

No one assigned

    Labels

    proposalThis issue suggests modifications. If it also has the "accepted" label then it is planned.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions