Live data from Hacker News

An intro to Zig's integer casting for C programmers

lagerdata.com

1–10 of 135 posts

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

#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 language, and yet there's an incredibly safe and expressive language that's used in high-reliability applications like missile guidance systems, that nobody seems to talk about.

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

#3
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-point math, and there is no single correct type for this value.

There are also some tricky choices in bit twiddling operations. Is it really such a problem when bits are shifted out of a value when this is normal behaviour down on the assembly level?

I guess I'll eventually learn to live with strict explicit conversion, and eventually I'll get better at picking the right type from the start, but I think implicit versus explicit conversion is an area where there is no "completely right" solution, because with 100% explicit conversions even coding down on the assembly level is more convenient ;)

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

#4
> You won't even get a warning unless -Wextra or -Weverything is turned on.

That's because -Wconversion isn't part of -Wextra for some reason. It is part of -Weverything though, and with that clang does warn:

  warning: implicit conversion changes signedness: 'int' to 'unsigned long' [-Wsign-conversion]
      if (sizeof(int) 

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

#5

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…

D doesn't try to "fix" C integer promotion/casts. It's because they aren't bad defaults. As a bonus you can port code from C with less risk. Soon you'll be able to compile it directly.

I contributed to found the only discrepancy in D vs C code wrt integer (-byte would yield byte instead of int), and it was fixed so that they match exactly C _conventions_.

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

#6
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…

> Make something idiot-proof and the world invents a better idiot.

Sure, but if you only ever give people safety scissors then you're severely limiting the kinds of things that they can build. You have to let people who know what they are doing be able to do what they need to do, so you have to be able to turn the runtime safety off. Zig can do this at the scope level.

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

#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

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

#8
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/item?id=19274244 (!!)

https://news.ycombinator.com/item?id=19274412

https://news.ycombinator.com/item?id=19770405

https://news.ycombinator.com/item?id=20776296

https://news.ycombinator.com/item?id=20934511 (!!) [3]

https://news.ycombinator.com/item?id=20939336 (!!)

https://news.ycombinator.com/item?id=21286061

https://news.ycombinator.com/item?id=21286292

https://news.ycombinator.com/item?id=21435869 (!)

https://news.ycombinator.com/item?id=23609499 (!!)

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

#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.

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

#10
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…

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've heard of Ada before, AFAICT the only context in which it is brought up is to downplay hype over Rust/Zig; never as a standalone.

Sounds like a PR problem, mostly.

EDIT: Not to say that there aren't other problems, just saying that this point alone isn't some magic bullet.

I don't like Rust because it gives me memory safety, tons of languages do that. I love Rust because the tradeoffs it gives me worth the switch, and the overall tooling and ecosystem are very well made.

Post reply on HN