jbe
July 17, 2025, 4:25pm
1
Hey, does anyone know what’s the “precedence” of comptime? It is not listed in the operator precedence table .
What I mean is, when I write, comptime a + b == c, then does that mean:
(comptime a) + b == c
(comptime (a + b)) == c
comptime (a + b == c)
?
Good question. I asked myself that as well!
This does not compile
const aa: usize = 42;
var bb: usize = 43;
bb += 1;
const check: bool = comptime aa == bb;
std.debug.print("{}", .{check});
jbe
July 17, 2025, 5:04pm
3
That means it has a lower precedence than ==, I guess. But… how low? Either way, I think it deserves some attention in the docs.
Yeah I bumped into a precedence thingy a while ago. Since then I do not think but just put brackets. Which is advisable anyway I believe.
Still it would be nice if it was documented.
jbe
July 17, 2025, 5:17pm
5
Unless someone can point out where it is documented, I can later open an issue, noting that it doesn’t seem to be documented (yet being relevant).
Side question: Is comptime (edit: in front of an expression) an operator, technically/semantically/…? Or not? I guess it’s a keyword, but not an operator. Though not sure.
dimdin
July 17, 2025, 5:45pm
6
You can use comptime in different contexts: to declare parameters, to evaluate entire blocks of code, and more.
For expressions it works similar to the return statement.
In your list, the last comptime (...) is the correct.
In the zig reference there is the zig grammar . Search for KEYWORD_comptime to find all the possible reductions of comptime.
2 Likes
jbe
July 17, 2025, 5:51pm
7
Sorry I didn’t clarify: I meant comptime in front of an expression (I edited the post).
I got until:
# An expression, assignment, or any destructure, as a statement.
VarDeclExprStatement
<- VarDeclProto (COMMA (VarDeclProto / Expr))* EQUAL Expr SEMICOLON
/ Expr (AssignOp Expr / (COMMA (VarDeclProto / Expr))+ EQUAL Expr)? SEMICOLON
But… that’s a bit hard to read for me right now
P.S.: Oh, I think that’s not the part I was supposed to be looking at. I guess the relevant part is:
PrimaryExpr
<- AsmExpr
/ IfExpr
/ KEYWORD_break BreakLabel? Expr?
/ KEYWORD_comptime Expr
/ KEYWORD_nosuspend Expr
/ KEYWORD_continue BreakLabel?
/ KEYWORD_resume Expr
/ KEYWORD_return Expr?
/ BlockLabel? LoopExpr
/ Block
/ CurlySuffixExpr
Does that imply that comptime always applies to the whole expression?