# Best way to print in comptime?

**URL:** <https://ziggit.dev/t/best-way-to-print-in-comptime/2179>\
**Category:** Help\
**Tags:** comptime\
**Created:** [November 10, 2023, 7:48pm UTC](https://ziggit.dev/t/best-way-to-print-in-comptime/2179 "2023-11-10T19:48:24Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![zraineri](https://ziggit.dev/user_avatar/ziggit.dev/zraineri/32/755_2.png) [@zraineri](https://ziggit.dev/u/zraineri)\
**Post date:** [November 10, 2023, 7:48pm UTC](https://ziggit.dev/t/best-way-to-print-in-comptime/2179/1 "2023-11-10T19:48:24Z")

</div>

I’m trying to print a buffer at comptime. In my use case, the user does not need file or calling information, they just need to see the provided text at comptime.

[compileError](https://ziglang.org/documentation/master/#compileError) adds noise but at least prints the buffer (unfortunately with an offset) the way I provide it.

```zig
file:0:0: error: 
                                
                                Invalid action. Expected one of the following:
                                    - option 1
                                    - option 2
        @compileError(msg);
        ^ ~~~~~~~~~~~~~~~~~
file:0:0: note: called from here
            print.message("\nInvalid action. Expected one of the following::\n. - option 1\n - option 2\n");

```

[compileLog](https://ziglang.org/documentation/master/#compileLog) also adds noise and doesn’t print the buffer the way I provide it.

```zig
file.zig:0:0: error: found compile log statement
        @compileLog(msg);
        ^ ~~~~~~~~~~~~~~~

Compile Log Output:
@as([]const u8, "\nInvalid action. Expected one of the following:\n. - option 1\n - option 2\n")

```

[std.io.getStdErr().writer().write](https://github.com/ziglang/zig/blob/9ad03b628f5d4770f9f26e646019292b5ae9cf9b/lib/std/io/writer.zig#L16) calls externs which fail in comptime

```zig
zig/std/os.zig:1135:32: error: comptime call of extern function
        const rc = system.write(fd, bytes.ptr, adjusted_len);
                   ~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
zig/std/fs/file.zig:1163:28: note: called from here
            return os.write(self.handle, bytes);
                   ~~~~~~~~^~~~~~~~~~~~~~~~~~~~
zig/std/io/writer.zig:17:27: note: called from here
            return writeFn(self.context, bytes);
                   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
file:0:0: note: called from here
        index += stderr.write(msg[index..]) catch return;
                 ~~~~~~~~~~~~^~~~~~~~~~~~~~
file:0:0: note: called from here
            print.message("\nInvalid action. Expected one of the following:\n. - option 1\n - option 2\n");

```

Is there anyway, at comptime, to just print to console the buffer I provide with nothing else printed? Is this possible to do when:

1. Wanting to also stop compilation
2. Wanting to continue compilation

Thanks!

Note: also the provided caller information isn’t helpful to end users anyway. It only shows where the compilerLog and compilerError is called from (which is my handler function)

---

<div class="post-metadata">

**Author:** ![tensorush](https://ziggit.dev/letter_avatar_proxy/v4/letter/t/6a8cbe/32.png) [@tensorush](https://ziggit.dev/u/tensorush)\
**Post date:** [November 10, 2023, 8:04pm UTC](https://ziggit.dev/t/best-way-to-print-in-comptime/2179/2 "2023-11-10T20:04:15Z")

</div>

Have you tried [`std.fmt.comptimePrint`](https://github.com/ziglang/zig/blob/0.11.0/lib/std/fmt.zig#L2052)?

---

<div class="post-metadata">

**Author:** ![IntegratedQuantum](https://ziggit.dev/user_avatar/ziggit.dev/integratedquantum/32/782_2.png) [@IntegratedQuantum](https://ziggit.dev/u/IntegratedQuantum)\
**Post date:** [November 10, 2023, 8:11pm UTC](https://ziggit.dev/t/best-way-to-print-in-comptime/2179/3 "2023-11-10T20:11:25Z")

</div>

As far as I know `@compileLog` and `@compileError` are the only functions that allow printing stuff to the terminal at compile-time.

Your only other option would be to do something within the build system, but that would make the whole thing pretty complicated.

May I ask why you want this? Especially the case were you don’t want to stop compilation seems weird to me. Couldn’t you just do a runtime print instead?

---

<div class="post-metadata">

**Author:** ![zraineri](https://ziggit.dev/user_avatar/ziggit.dev/zraineri/32/755_2.png) [@zraineri](https://ziggit.dev/u/zraineri)\
**Post date:** [November 10, 2023, 8:31pm UTC](https://ziggit.dev/t/best-way-to-print-in-comptime/2179/4 "2023-11-10T20:31:40Z")

</div>

I hadn’t, but it looks like it handles the formatting during comptime then provides the buffer which I then still have to get into the console. I’ve already handled all the formatting and am having trouble getting my formatted buffer printed to console

```zig
pub inline fn comptimePrint(comptime fmt: []const u8, args: anytype) *const [count(fmt, args):0]u8 {
    comptime {
        var buf: [count(fmt, args):0]u8 = undefined;
        _ = bufPrint(&buf, fmt, args) catch unreachable;
        buf[buf.len] = 0;
        return &buf;
    }
}

```

Definitely a good resource though for anyone needing to format a string at comptime, I didn’t know this existed

---

<div class="post-metadata">

**Author:** ![zraineri](https://ziggit.dev/user_avatar/ziggit.dev/zraineri/32/755_2.png) [@zraineri](https://ziggit.dev/u/zraineri)\
**Post date:** [November 10, 2023, 8:44pm UTC](https://ziggit.dev/t/best-way-to-print-in-comptime/2179/5 "2023-11-10T20:44:38Z")

</div>

> [@IntegratedQuantum](#):
>
> As far as I know `@compileLog` and `@compileError` are the only functions that allow printing stuff to the terminal at compile-time.

That’s unfortunately what I thought

> [@IntegratedQuantum](#):
>
> Your only other option would be to do something within the build system, but that would make the whole thing pretty complicated.

Yeah I might do that at a later date. Do you have any idea on where to edit the compileLog functionality? It’s almost what I need so I might branch and edit it

> [@IntegratedQuantum](#):
>
> May I ask why you want this? Especially the case were you don’t want to stop compilation seems weird to me. Couldn’t you just do a runtime print instead?

Compile-time syntax analysis of an inlined query language with configurable hints/warnings/errors. Not stopping compilation would be the hints/warnings. I could do a runtime print and then immediately abort for errors but when I already have information at comptime I’d very much prefer not affect runtime code at all and just fail to compile. [In Rust I was accomplishing this with macros](https://doc.rust-lang.org/book/ch19-06-macros.html). I can’t show it off just yet but it would make more sense why I’m doing it this way if I could. I’ll be showcasing it along with the rest of the software on here once we launch our public beta here soon.

Edit: Also I personally hate the workflow of let’s fix this and see what breaks next. If I can provide multiple errors at once then stop compilation after providing all the necessary information I’d much prefer it

---

<div class="post-metadata">

**Author:** ![zraineri](https://ziggit.dev/user_avatar/ziggit.dev/zraineri/32/755_2.png) [@zraineri](https://ziggit.dev/u/zraineri)\
**Post date:** [November 11, 2023, 6:36am UTC](https://ziggit.dev/t/best-way-to-print-in-comptime/2179/6 "2023-11-11T06:36:58Z")

</div>

I ended up branching zig and implemented a fairly hacky change to compileLog’s behavior to actually print the provided buffer and remove the file info, error info etc.

This

```sh
file.zig:0:0: error: found compile log statement
        @compileLog(msg);
        ^ ~~~~~~~~~~~~~~~

Compile Log Output:
@as([]const u8, "\nInvalid action. Expected one of the following:\n - option 1\n - option 2\n")

```

is now:

```sh
Invalid action. Expected one of the following:
  - option 1
  - option 2

```

This is obviously not a long term plan as I don’t expect end users to use a non-official branch of Zig, but it gives me an opportunity to see if this is as valuable as I think it is.

Does anyone know if there’s been any discussion around something like a “@compilePrint” that effectively mimics [std.debug.print](https://github.com/ziglang/zig/blob/97e23896a9168132b6d36ca22ae1af10dd53d80d/lib/std/debug.zig#L84) but at compilation?

---

<div class="post-metadata">

**Author:** ![squeek502](https://ziggit.dev/user_avatar/ziggit.dev/squeek502/32/409_2.png) [@squeek502](https://ziggit.dev/u/squeek502)\
**Post date:** [November 11, 2023, 8:39am UTC](https://ziggit.dev/t/best-way-to-print-in-comptime/2179/7 "2023-11-11T08:39:54Z")

</div>

My understanding is that Zig very intentionally does not have warnings, so something that would allow user code to emit warnings during compilation would likely be considered a misfeature.

The most relevant issue is probably [Don't stop compilation if compileLog is used · Issue #5469 · ziglang/zig · GitHub](https://github.com/ziglang/zig/issues/5469)

---

<div class="post-metadata">

**Author:** ![zraineri](https://ziggit.dev/user_avatar/ziggit.dev/zraineri/32/755_2.png) [@zraineri](https://ziggit.dev/u/zraineri)\
**Post date:** [November 11, 2023, 9:14am UTC](https://ziggit.dev/t/best-way-to-print-in-comptime/2179/8 "2023-11-11T09:14:02Z")

</div>

Ah that’s unfortunate. I definitely understand Zig itself not utilizing warnings, but I feel like forcing the entire ecosystem to be unable to do so would be a bit of an overstep and unnecessarily limiting. This feature certainly could be used to generate warnings but I’m sure it would have many other use-cases as well

Thank you for sharing the discussion link!
