Live data from Hacker News

An intro to Zig's integer casting for C programmers

lagerdata.com

121–130 of 135 posts

Re: An intro to Zig's integer casting for C programmers

#121
post #58

I am curious to know the reasons behind the @as() syntax. What's wrong with "i32 y = (i32) x;"? I look with interest to new languages but after so many years dealing with C, my parser crashes when I see the type after the variable name and at the end of functions declarations. var x : u8 = 5; What's wrong with "u8 x = 5;"? And I don't really like type inference very much (or when it's abused or cannot be avoided). Wh…

> What's wrong with "u8 x = 5;"? I believe it makes the parser marginally more complicated. Let's say you had a non-builtin type, "foo". The var trigger immediately declares what's going on. For "foo x = 5;" the parser must either 1) have contextual information that foo is a declared type or 2) wait to notice that there are two identifiers side-by-side and then resolve that this means "it must be a variable declarati…

> I believe it makes the parser marginally more complicated

This would mean that the "peace of mind" of the Zig compiler developer has priority over my own "peace of mind". That's not ok.

Is the compile developer reasoning "I don't care if the syntax is verbose, messy (or whatever), it just makes my job easier"?

> Honestly, C and C++ are very much in the minority for choosing this syntax

Java, C#, Objective-C....

Re: An intro to Zig's integer casting for C programmers

#122
post #87
post #81

Earlier quoted context omitted.

juxtaposition (i.e. syntax that has meaning when two identifiers stand together separated only by whitespace) is generally a problem for parsing. Basically, it introduces all kinds of parsing ambiguities. Compare the following: a *b a * b a[n] x b [n] a y a y e(1) f e (1)f Even for C/C++ this is complicated (you need the symbol table when parsing, to know if an identifier is a type or not), but for more advanced lang…

> you can always also write the expected type, if you want. But you have to read the code of people who didn‘t.

Exactly. Like when you see:

    var x = some_function();
What am I dealing with here? mmmmmm....

Ok... let's find some_function()... I hope my editor has "go to definition", otherwise "grep -r some_function ."...

Re: An intro to Zig's integer casting for C programmers

#123
post #120
post #58

I am curious to know the reasons behind the @as() syntax. What's wrong with "i32 y = (i32) x;"? I look with interest to new languages but after so many years dealing with C, my parser crashes when I see the type after the variable name and at the end of functions declarations. var x : u8 = 5; What's wrong with "u8 x = 5;"? And I don't really like type inference very much (or when it's abused or cannot be avoided). Wh…

> I am curious to know the reasons behind the @as() syntax. What's wrong with "i32 y = (i32) x;"? Built-ins in Zig are functions beginning with `@`. Your cast operator would be a new syntax construct, something Zig tries to limit as much as possible (keeping the parser simple). > What type is "b"? i32? That's easy, i32. Integer literals are of type comptime_int, which may coerce into other integer types if possible w…

> That's easy.

For code that is not yours, or code that you open a year later, I think it's easier to specify the type. Exactly as when you see something like "var x = some_function();" and you need to know the type.

Re: An intro to Zig's integer casting for C programmers

#124

Earlier quoted context omitted.

I loved reading about ada back in the day, how is it nowadays wrt network programming. I'm checking out gnat.sockets, does it have a snappy select() type thing like linux's epoll? Sorry if a lazy question, last time I looked at ada was decades ago and I'm surprised to find enthusiasts for it on HN. edit: an advantage for c is that you can get loads of examples just googling, sucks for less used languages I guess.

It has been a while, but you may want to look at: - https://en.wikibooks.org/wiki/Ada_Programming/Libraries/GNAT... (search for "C select()") - https://www.codelabs.ch/anet/ - https://github.com/samueltardieu/adasockets - https://github.com/rtyler/ada-playground/blob/master/epollec... Some old reading: https://brokenco.de/2013/04/07/async-with-ada.html You may want to check how GNAT.Sockets and Anet are implemented,…

thanks man, your post is a great timesaver, that wikibooks link itself is packed with info!

Re: An intro to Zig's integer casting for C programmers

#125
post #118
post #49

Earlier quoted context omitted.

C# has GC by default, but you can do manual memory management in "unsafe" blocks

You can also do manual memory management in safe code. - value types - stackalloc is safe since C# 7 - SafeHandles since C# 2.0 - IDispose interface - IDispose like Dispose() since C# 8 - Marshal class since C# 1.0 - Span () classes since C# 7

Forgot to mention:

- static lambdas since C# 9

- native function pointers since C# 9

Re: An intro to Zig's integer casting for C programmers

#126
post #115
post #10

Earlier quoted context omitted.

Not to sound too aggressive, I'm not sure why ada programmers keep being surprised at the hype. Safety (of all kinds) is not the only selling point of Rust and Zig, and the fact that Ada also have safety measures doesn't mean that it's instantly an option for me. For example, I keep seeing people hype about Zig/Rust, for all kinds of reasons; and the way that they present their arguments is very compelling. While I'v…

Back when Ada was as young as Rust and Zig, we only had magazines like BYTE and Dr Dobbs, and BBSs for driving hype. There were no free beer compilers and no 8/16 bit home computer was capable of hosting an Ada compiler, Basic, Pascal and Modula-2 was the best they could manage.

Sorry to hear that

Re: An intro to Zig's integer casting for C programmers

#127
post #123
post #120

Earlier quoted context omitted.

> I am curious to know the reasons behind the @as() syntax. What's wrong with "i32 y = (i32) x;"? Built-ins in Zig are functions beginning with `@`. Your cast operator would be a new syntax construct, something Zig tries to limit as much as possible (keeping the parser simple). > What type is "b"? i32? That's easy, i32. Integer literals are of type comptime_int, which may coerce into other integer types if possible w…

> That's easy. For code that is not yours, or code that you open a year later, I think it's easier to specify the type. Exactly as when you see something like "var x = some_function();" and you need to know the type.

Best practice for languages with this "var" approach is to use it when the type is obvious or irrelevant and to explicitly declare the type when it is non-obvious and relevant.

If you are reviewing code and can't instantly tell what the type is for a variable, ask the author to explicitly include the type. Problem solved.

Re: An intro to Zig's integer casting for C programmers

#128

Earlier quoted context omitted.

From what I can tell, safety isn't a selling factor of Zig. From a "safety" perspective Zig seems like a step backwards compared to the latest generation of languages, and Rust in particular. Zig's ergonomics seem decent but its memory safety tact appears to basically be to include valgrind-like tools into debug builds with good PR.

Putting valgrind into the stdlib is really clever, and also, I like memory safety being the carrot to get you to write tests. I worry having a 'safe' system like rust sometimes causes very smart (TM) developers, especially less experienced ones, to be complacent and write less tests.

Except I can already do that in C++.

https://devblogs.microsoft.com/cppblog/new-static-analysis-r...

https://devblogs.microsoft.com/cppblog/finding-bugs-with-add...

https://docs.microsoft.com/en-us/cpp/code-quality/using-sal-...

https://docs.microsoft.com/en-us/visualstudio/debugger/how-t...

No need to switch languages with a lesser eco-system, just learning to turn on the compiler switches that are already there.

Re: An intro to Zig's integer casting for C programmers

#129
post #85

Earlier quoted context omitted.

I'm not saying it's justified, but I think the syntax is also a big impediment (just like for my daily driver, OCaml). Ada looks pretty verbose and annoying to write code in, at least if one wants to use it for side projects, small utilities, etc. rather than spaceship firmware; and that's probably how a lot of languages become popular, you need to tinker with them on small things. OCaml has seen the alternative "Rea…

My opinion: I really dislike Rust's syntax because to me it is similar to Perl's that I also do not like that much as I do not know what is happening just by looking at it (I still like to use Perl here and there, but these days I lean towards Tcl more), it seems hidden from me, even compared to something as C where I have to think about conversions and whatnot. I can easily read and understand someone's Ada, C, or O…

Thank you for the answer :-). I'm still surprised because to me, Rust looks a _lot_ like OCaml with curly braces. The surface lexical conventions are mixed with C++ (like `::` for namespacing) but code still looks more like OCaml than C, with `let` bindings, sum types, expressions, `match` being omnipresent. Even the type annotations look more ML-ish than the type-first C convention. I fail to see the relation to perl (there's barely any `$` in rust code! and most sigils are still the & related to references that also abound in C).

Re: An intro to Zig's integer casting for C programmers

#130
post #114
post #100

Earlier quoted context omitted.

few UNIX vendors that cared to offer compiler like Sun, it was an additional acquisiation on top of UNIX SDK. The Sun C compiler was an additional cost item, just like everyone else.

Indeed, so if you already had to pay for C which is a required language on any UNIX, there is no way around it, why spend the extra dollars for Ada just to feel good? That was my point.

Because it's a better language for a lot of use cases? But I concede that the history of the industry indicates that "don't care" is the norm.
Post reply on HN