logoalt Hacker News

dlcarrierlast Tuesday at 8:40 PM5 repliesview on HN

I just looked up a Hello World program from the Zig Wikipedia article:

    const std = @import("std");
    const File = std.Io.File;
    
    pub fn main(init: std.process.Init) !void {
        _ = try File.stdout().writeStreamingAll(init.io, "Hello, World!\n");
    }
That's a lot to follow, just to output a plan-text message, especially after this line: "The primary goal of Zig is to be a better solution to the sorts of tasks that are currently solved with C. A primary concern in that respect is readability…"

Replies

jdlshorelast Tuesday at 8:44 PM

I don’t think you can judge a programming language based on its “Hello World.” AppleSoft BASIC has this:

10 PRINT “Hello World”

Beautifully simple and readable. But it’s not a good language by modern standards.

In your example, I see a lot of complexity being surfaced: output streams, locals instead of globals, error handling. I don’t know Zig but all of those are things that are important to address, and I like that the example doesn’t sweep them under the rug in pursuit of a false readability.

gracefullibertylast Tuesday at 10:19 PM

Everything it's doing it clear and readable. It's just not as easy to write. It streams the bytes to stdout using the default IO interface, and it can fail.

Alternatively, here's a simpler version (prints to stderr).

    const std = @import("std");

    pub fn main() void {
        std.debug.print("Hello, world!\n", .{}); 
    }
In practice, you normally don't want to print messages to stdout. So the increased friction here actually pushes you in a better direction.
show 1 reply
defenlast Tuesday at 8:42 PM

The issue is that most "hello world" programs are not correct.

show 1 reply
tester756last Tuesday at 9:16 PM

Whats the point of evaluating technology from hello world programs?

flohofwoeyesterday at 8:53 AM

That's the 'official' hello world which is indeed a bit verbose (it's "correct" in the way that it generally shows how to stream formatted text to stdout though).

Arguably this is the more beginner friendly version, this prints to stderr though:

hello.zig:

    const print = @import("std").debug.print;

    pub fn main() void {
        print("Hello World!\n", .{});
    }
...and then

    zig run hello.zig