kw comptime
Reserved keyword
The list of words: §1.4
Syntax
Fn = [ "comptime" [ "literal" ] | "unsafe" ] "fn" Ident [ Generics ] "(" [ Params ] ")"
[ "->" Type ] [ Effects ] [ Where ] { Contract } ( Block | "=>" Expr ) ;Primary = Literal | Path | "it" | "self" | "(" Expr { "," Expr } ")" | ListLit | MapLit
| StructLit | Lambda | Block | If | Match | For | While | Loop | Scope | Select | Query
| "spawn" ( Postfix | Block ) | "await" Unary | "atomic" [ "(" Args ")" ] Block
| "return" [ Expr ] | "fail" Expr | "yield" Expr | "respond" Expr | "break" [ Label ] [ Expr ]
| "continue" [ Label ] | "emit" Path "(" Expr ")" | "unsafe" Block | "blocking" Block
| "comptime" Block | "." Ident [ "(" [ Args ] ")" ] ;Grammar: App. A
Defined in
- 15.1 comptime — A comptime fn is executed only at compile time; comptime { …
- 15.4 User-defined attributes and code generation — attr Name { fields } declares an attribute; comptime fn Name.apply(d
- 1.4 Keywords — Reserved (cannot be used as names)
- 4.5 impl and methods — Inside a method self and Self are available.
- 15.2 Reflection — reflect(T) -> TypeInfo (comptime only)
- 15.3 Prefixed literals — comptime literal fn re(s
Examples
attr Audit { level: Level = Level.Info }
comptime fn Audit.apply(d: Decl, a: Audit) -> Decl | CompileError {
d.wrapBody(body => quote { log.at($a.level, "call {$d.name}"); $body })
}— 15.4 User-defined attributes and code generation
comptime fn toCsvRow<T>(x: &T) -> String {
var out = String.new()
inline for f in reflect(T).fields where f.visible {
out.push("{x.[f]},")
}
out
}Related diagnostics
comptime-error, not-comptime, impl-foreign-type, unknown-method, prefix-literal