News for hackers. Stuff that matters.
Re:Zig is everything I want C to be by up2isomorphism on Tuesday October 22, 2024 @12:57PM (#41916250)
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?

Re:Zig is everything I want C to be by jonathrg on Tuesday October 22, 2024 @01:17PM (#41916432)
C's function declaration syntax is pretty weird and makes the language harder to parse. Most languages have a function keyword.
Re:Zig is everything I want C to be by tmtvl on Tuesday October 22, 2024 @01:04PM (#41916321)
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).

Re:Zig is everything I want C to be by jiripospisil on Tuesday October 22, 2024 @01:54PM (#41916845)
> 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).

Re:Zig is everything I want C to be by fragmede on Tuesday October 22, 2024 @04:23PM (#41918325)
> 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?

Re:Zig is everything I want C to be by jiripospisil on Tuesday October 22, 2024 @05:37PM (#41918990)
> 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).

Re:Zig is everything I want C to be by TUSF on Tuesday October 22, 2024 @02:05PM (#41916951)
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.
Re:Zig is everything I want C to be by skavi on Tuesday October 22, 2024 @01:14PM (#41916414)
Re:Zig is everything I want C to be by znpy on Tuesday October 22, 2024 @03:11PM (#41917615)
> 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.

Re:Zig is everything I want C to be by tmtvl on Wednesday October 23, 2024 @09:09AM (#41924707)
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.
Re:Zig is everything I want C to be by znpy on Thursday October 24, 2024 @03:38PM (#41938956)
We had ctags for lack of better alternatives, not because it’s any good
Re:Zig is everything I want C to be by g-b-r on Wednesday October 23, 2024 @11:54PM (#41931748)
lsp can be extremely slow, on constrained devices
Re:Zig is everything I want C to be by c2xlZXB5Cg1 on Tuesday October 22, 2024 @02:16PM (#41917078)
I'd go with for loop syntax:

    for (string, 0..) |character, index| {
        _ = character;
        _ = index;
    }
Re:Zig is everything I want C to be by d_tr on Tuesday October 22, 2024 @01:54PM (#41916842)
Probably not much, but this is not the only change the language brings.
Re:Zig is everything I want C to be by ksp-atlas on Tuesday October 22, 2024 @04:09PM (#41918175)
As someone who wrote C for my hobby projects, I've switched to Zig and enjoy it more as it feels I can express my intention more clearly
Re:Zig is everything I want C to be by elteto on Tuesday October 22, 2024 @01:42PM (#41916718)
15 minutes… the horror.
Re:Zig is everything I want C to be by audunw on Tuesday October 22, 2024 @06:45PM (#41919485)
If you have a lot of experience with C, and take just a glance at the basics of Zig, I think you’ll understand why each of those changes were made. C doesn’t have very good syntax. Nether for compiler (correctly interpreting the code requires a lot of context), nor code editor, nor for humans.

If you have experience with some modern languages, you’ll definitely understand the syntax. Zig is close to a style of syntax that many newer languages seem to have settled on, and there are good reasons why that style has become prefer.

Why make a new language if you’re not going to make some improvements over the old? Seems like a very weird complaint to me.

Re:Zig is everything I want C to be by jhhh on Tuesday October 22, 2024 @09:06PM (#41920533)
Are you saying you don't like isomorphisms?