←back to thread

55 points atomlib | 10 comments | | HN request time: 0.955s | source | bottom
Show context
up2isomorphism ◴[] No.41916250[source]
What is the benefit of changing

int main(void) {

to:

pub fn main() !void {

Except making it looks like a "new" language and requires other people spends 15min to get to used to it?

replies(7): >>41916321 #>>41916432 #>>41916718 #>>41916842 #>>41917078 #>>41919485 #>>41920533 #
1. tmtvl ◴[] No.41916321[source]
There are tradeoffs:

Use of the pub keyword means you can tell whether a function is public in the module without needing to look at a separate place for listing exports (like, say, a header file). Though that itself has tradeoffs, so it's hard to say whether one or the other is better.

Use of the fn keyword makes it easy to find function definitions by grepping for it, whereas finding functions in regular C syntax needs semantic awareness.

What I don't understand is the !void at the end of the declaration, if we're meant to be returning an int, surely !int would be the expected syntax (although I would prefer returns int instead).

replies(4): >>41916414 #>>41916845 #>>41916951 #>>41917615 #
2. skavi ◴[] No.41916414[source]
https://ziglang.org/documentation/0.13.0/#Hello-World
3. frankjr ◴[] No.41916845[source]
> What I don't understand is the !void at the end of the declaration, if we're meant to be returning an int, surely !int would be the expected syntax (although I would prefer returns int instead).

`!void` means the function can return an error (e.g. return error.BadArgument) but doesn't produce any value itself. In the case of an error in the main function, the process will exit with 1, otherwise 0. The main function can also return a value directly (return type u8 / !u8).

replies(1): >>41918325 #
4. TUSF ◴[] No.41916951[source]
In Zig, the main function can return either a u8 or void (which always returns 0) or it can return an error union !void, where if you allow an error to bubble up to main, it'll print an error trace and return 1.
5. znpy ◴[] No.41917615[source]
> Use of the fn keyword makes it easy to find function definitions by grepping for it, whereas finding functions in regular C syntax needs semantic awareness.

does it matter though, in 2024 and beyond?

we've got clangd and lsp, grepping for functions is dumb, whether you're grepping C or grepping Zig.

replies(2): >>41924707 #>>41931748 #
6. fragmede ◴[] No.41918325[source]
> In the case of an error in the main function, the process will exit with 1, otherwise 0. The main function can also return a value directly (return type u8 / !u8).

We know that by convention, but how do we know that from

    pub fn main() !void {
If if write

    pub fn foo() !void {
will that function also get to return a u8?

Also, what happened to argv/argc?

replies(1): >>41918990 #
7. frankjr ◴[] No.41918990{3}[source]
> will that function also get to return a u8?

No, the main function (the entry point of the entire program) is special cased. Have a look at the source code. There you can see it's calling the user defined main function and handling its return value / error.

https://github.com/ziglang/zig/blob/2d888a8e639856e8cb6e4c6f...

> Also, what happened to argv/argc?

You can access argv with std.os.argv which is a slice of null terminated strings. It's better to go with std.process.argsAlloc though (requires an allocation but works on all supported platforms).

8. tmtvl ◴[] No.41924707[source]
We've had Ctags since 1992, so being able to grep for functions hasn't been important for 32 years... but there's still people who prefer to use grep to look for functions, so having a specific keyword is beneficial for them. It may also make the lex/parse/generate cycle simpler, but I'm not a compiler engineer, so I don't know for certain whether it does.
replies(1): >>41938956 #
9. g-b-r ◴[] No.41931748[source]
lsp can be extremely slow, on constrained devices
10. znpy ◴[] No.41938956{3}[source]
We had ctags for lack of better alternatives, not because it’s any good