Live data from Hacker News

An intro to Zig's integer casting for C programmers

lagerdata.com

11–20 of 135 posts

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

#11
post #7
post #2

> If runtime safety is turned off, you get undefined behavior You already know someone will teach their students to always have it off because its "slower" or something. Make something idiot-proof and the world invents a better idiot. Another language that does safety like this incredibly well is Ada (Ada/SPARK), and I'm unsure why people aren't more hyped about it. So many people hype Rust or Zig or whatever new lan…

Or Modula-2, NEWP, BASIC, or really most languages that aren't copy paste compatibel with C. Ada suffered from its domain, original price of compilers, and the few UNIX vendors that cared to offer compiler like Sun, it was an additional acquisiation on top of UNIX SDK. Everyone knows that only newbies do coding errors in C, so why spend the extra money? /s

> Everyone knows that only newbies do coding errors in C, so why spend the extra money? /s

The Ariane 5 maiden flight disaster illustrated quite clearly that the choice of language has little influence on the actual correctness of a program.

Ada on its own is no better than Pascal in that regard. A formally verified and thoroughly tested MISRA C program can be safer and more correct than a sloppily written Ada program.

So the question is indeed not as rhetorical as one might think - why spend the extra money indeed? Isn't it better spent on verification, testing, tooling and culture, which benefits the development regardless of programming language?

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

#12

IME the strict conversion rules in Rust and Zig can be quite a bummer for somebody coming from C because they may add a surprising amount of friction in day-to-day coding. Yes, C code is often way too sloppy when it comes to picking the right type (signed vs unsigned vs float), and it conveniently hides the problems if the wrong choice was made. But sometimes the same value needs to be used in integer and floating-po…

You basically have to accept the relatively small, upfront cost of verbose declarations for these sorts of things in exchange for the lack of headaches + bugs you wind up with later.

Or not, you can also choose not to write these kinds of languages and nobody can blame you -- to each their own.

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

#13
post #2

> If runtime safety is turned off, you get undefined behavior You already know someone will teach their students to always have it off because its "slower" or something. Make something idiot-proof and the world invents a better idiot. Another language that does safety like this incredibly well is Ada (Ada/SPARK), and I'm unsure why people aren't more hyped about it. So many people hype Rust or Zig or whatever new lan…

I think I'd like to get into Ada more, but I'm always a bit unsure on how to select the right toolchain (is the Dragonegg/LLVM option viable today?) etc.

I would love to see something like "Rustlings" for Ada, as I found that was a good way to practice not only writing code, but reading it as well.

I was able to self-teach Haskell and Erlang without any major problems, and even managed to ship applications written in it commercially.

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

#15
post #11
post #7

Earlier quoted context omitted.

Or Modula-2, NEWP, BASIC, or really most languages that aren't copy paste compatibel with C. Ada suffered from its domain, original price of compilers, and the few UNIX vendors that cared to offer compiler like Sun, it was an additional acquisiation on top of UNIX SDK. Everyone knows that only newbies do coding errors in C, so why spend the extra money? /s

> Everyone knows that only newbies do coding errors in C, so why spend the extra money? /s The Ariane 5 maiden flight disaster illustrated quite clearly that the choice of language has little influence on the actual correctness of a program. Ada on its own is no better than Pascal in that regard. A formally verified and thoroughly tested MISRA C program can be safer and more correct than a sloppily written Ada progra…

Ariane 5 error was caused by the remaining 30% programming errors when we leave out the 70% of software failures caused by C typical errors.

So yes, it is quite worthy to reduce the amount of money spent in verification, testing, tooling and culture.

The alternative is to just give up that programmers will never learn and just force verification at hardware level, like Google is doing on Android.

"Memory Tagging for the Kernel: Tag-Based KASAN"

https://www.youtube.com/watch?v=f-Rm7JFsJGI

Oracle on Solaris SPARC,

https://docs.oracle.com/cd/E37838_01/html/E61059/gqajs.html

Apple on iOS,

https://developer.apple.com/documentation/security/preparing...

Microsoft on Azure Sphere,

https://www.microsoft.com/security/blog/2020/11/17/meet-the-...

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

#16
It says Zig eliminates "implicit conversions unless they are guaranteed to be safe (for example, assigning a u8 value to a u16 variable cannot fail or lose data)", but then suggests that @as should be used when "casting an int to a larger-size int of the same sign". Why should @as be used if it's safe to implicitly convert?

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

#17
post #2

> If runtime safety is turned off, you get undefined behavior You already know someone will teach their students to always have it off because its "slower" or something. Make something idiot-proof and the world invents a better idiot. Another language that does safety like this incredibly well is Ada (Ada/SPARK), and I'm unsure why people aren't more hyped about it. So many people hype Rust or Zig or whatever new lan…

I think I'd like to get into Ada more, but I'm always a bit unsure on how to select the right toolchain (is the Dragonegg/LLVM option viable today?) etc. I would love to see something like "Rustlings" for Ada, as I found that was a good way to practice not only writing code, but reading it as well. I was able to self-teach Haskell and Erlang without any major problems, and even managed to ship applications written in…

Plenty of learning paths at https://learn.adacore.com/

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

#18
post #9

IME the strict conversion rules in Rust and Zig can be quite a bummer for somebody coming from C because they may add a surprising amount of friction in day-to-day coding. Yes, C code is often way too sloppy when it comes to picking the right type (signed vs unsigned vs float), and it conveniently hides the problems if the wrong choice was made. But sometimes the same value needs to be used in integer and floating-po…

C programmers are well known to refer to such programming safety as straighjacket since the Pascal days anyway. There was more than enough time to learn why it was the right option to start with.

The straight-jacket is the syntax in Ada. I don't feel locked in by the correctness measures, but if you are going to have elaborate declaration blocks I prefer "let foo = bar in ...".

Because everything else is "let" in disguise. ML got the syntax right.

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

#19
post #2

> If runtime safety is turned off, you get undefined behavior You already know someone will teach their students to always have it off because its "slower" or something. Make something idiot-proof and the world invents a better idiot. Another language that does safety like this incredibly well is Ada (Ada/SPARK), and I'm unsure why people aren't more hyped about it. So many people hype Rust or Zig or whatever new lan…

Yeah, and Ada/SPARK performs those checks people are hyped about (and more) at compile-time! Honestly, Ada does everything those languages do (regarding safety), and does more and does it better (again, talking about safety and correctness here). If you want evidence, check out my posts: https://news.ycombinator.com/item?id=19122884 (!) https://news.ycombinator.com/item?id=19245898 (!!) https://news.ycombinator.com/i…

Most of Rust's checks are at compiletime, and Ada employs runtime checks as well.

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

#20

It says Zig eliminates "implicit conversions unless they are guaranteed to be safe (for example, assigning a u8 value to a u16 variable cannot fail or lose data)", but then suggests that @as should be used when "casting an int to a larger-size int of the same sign". Why should @as be used if it's safe to implicitly convert?

Hmm, this indeed doesn't make much sense. Assigning to a wider type with or without different signedness doesn't require a cast as long as all bits fit into the new type:

https://www.godbolt.org/z/x8a65sPcr

Post reply on HN