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.
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.The C Programming Language includes a Hello World example that calls printf without checking the return value and returns a success code from the main function regardless.
The first example I find when googling "java hello world" simply calls System.out.println and neglects to call System.out.checkError to see if it was successful before exiting with a success code. Some Java developers won't even know what I'm talking about here because it has never occurred to them that printing may fail in a way that can only be discovered through this weird checking mechanism.
Go's example from their getting started guide simply calls fmt.Println while ignoring the return values which include any error that may have occurred, and the program exits with a success code regardless.
The example from Rust by Example is at least correct and thorough in that it will predictably panic upon error when invoking the println! macro, which is documented, but will through that mechanism not give you the option to actually handle the error except by using a different mechanism which front-loads more of the complexity (e.g. writeln!(io::stdout(), "Hello World")? for something equivalent to the Zig example).
Of course for something as basic as Hello World it might be easy to tell whether it was successful through a quick glance at the output, but consider some of these limitations in a larger program.
So maybe there is more inherent complexity to this problem than a typical Hello World implementation will reveal. Add to that the complexity of Zig's new swappable I/O models and their Hello World isn't so absurd.
> and how likely is that to fail anyway (at least I never had the canonical C hello-world fail on me).
I don't expect to know how likely writing to stdout is to fail and I don't think any answer to that question other than 0 really warrants ignoring the potential error if the correct result of invoking your program depends on it. For what it's worth, at least in Unix-likes, stdout could be pretty much anything. Writing to stdout could fail because a switch at the user's ISP is rebooting.
#include <stdio.h>
int main(void)
{
return printf("hello, world\n");
}
I just tested it in a Bash shell, and it works great, only adding a single word, with clear functionality, to the example.Explicitly passing IO in is a fine design choice, but it's not a correctness issue to say others are wrong to not do so.
Should they?
If hello world is meant to be a simplification of printing large amounts of text to a buffered standard out? Then yes, it probably should be checking errors.
If hello world is meant to be a simplification of low-volume debug logging to prove that code was reached (aka, printf debugging), then the simple alternative hello world using std.debug.print is what you want.
For such debug prints, you don't want any buffering, you don't want it mixed in with stdout (despite the name, stderr is not just for error messages), and you don't really need to check for errors. And std.debug.print does not return errors.
#include <stdio.h>
int main(void)
{
return printf("hello, world\n");
}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