Live data from Hacker News

An intro to Zig's integer casting for C programmers

lagerdata.com

91–100 of 135 posts

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

#91

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…

I sort of felt similarly when first using Zig until it crashed with a helpful error at runtime when I was trying to cast -1 to an unsigned integer :) The debugging time that saved more than outweighed the amount of time it took to do the explicit cast.

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

#92
post #85

Earlier quoted context omitted.

> 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. Same applies to the tooling of Ada/SPARK, but as you have said, it is pretty much a PR issue.

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…

> Ada looks pretty verbose and annoying to write code in,

I still dont understand this, you spend 80% of the time thinking about code, 19% reading, and 1% typing.

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

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

> Sounds like a PR problem, mostly. It's the case that the Zig creator did put a high priority on soft "PR" for the language, the first hire was a developer advocate/community manager. I think this is a great case of "lessons learned" from the history of programming languages.

[deleted]

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

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

   var x : u8 = 5;
allows making type optional while keeping the code about the same, e.g.

   var x = 5;
This arrangement has this nice consistency to it, with the type info being more of a hint (to the compiler or to the programmer) than a required component.

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

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

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.

Not a Zig expert, but safety is a factor for Zig, it just treats it as less of an absolute than Rust. I think the thing to keep in mind is that something can be a priority without being an absolute priority. I'd make a comparison to OpenBSD vs Linux. Both have security as a priority, OpenBSD just has a more absolute focus on it.

For example, a couple of features come together really nicely to make memory safety easier to test in Zig: * You need a reference to an Allocator to be able to allocate memory, so as a general rule, the caller can control which allocator is used. * Unit testing is integrated well into the language. * Therefore, you can create an allocator for each unit test, and fail the test at the end if any memory was leaked. * This process can also happen at the application level with the General Purpose Allocator, which can let you print an error when the program exits if anything was leaked.

The above doesn't solve every memory safety problem (and there are other features like native bounds-checked slices that solve other kinds of issues), but it provides an extra layer that can probably get us quite far into the "quite safe" camp.

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

#96
post #37
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…

> incredibly safe […] high-reliability applications like missile guidance systems https://en.m.wikipedia.org/wiki/Cluster_(spacecraft)

Yup, people are the issue.

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

#97
post #92
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…

> Ada looks pretty verbose and annoying to write code in, I still dont understand this, you spend 80% of the time thinking about code, 19% reading, and 1% typing.

I never used Ada, but read a book on it.

I don't even remember much, just that how much I loved just the look of the example programs, but that was during community college when I was new to programming. It wasn't part of a class, but the guy who implemented the curriculum just jammed the library with jewels and the entire library collection was just full of classics especially since the actual curriculum was just a basic associates in "business data processing."

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

#98
post #92
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…

> Ada looks pretty verbose and annoying to write code in, I still dont understand this, you spend 80% of the time thinking about code, 19% reading, and 1% typing.

80% thinking when you're stuck and don't know who to ask. I've made the same mistake. Have you watched some streamers? They are prototyping by typing, I would say, 50% of the time.

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

#99
post #92
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…

> Ada looks pretty verbose and annoying to write code in, I still dont understand this, you spend 80% of the time thinking about code, 19% reading, and 1% typing.

I don't have this experience. I like to prototype by writing code (or type signatures), and then refactor a lot from the initial solution. That's easier to do if the syntax is reasonably concise (the other half is to have good types which Ada definitely provides).

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

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

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.

Post reply on HN