Live data from Hacker News

An intro to Zig's integer casting for C programmers

lagerdata.com

111–120 of 135 posts

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

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

> You already know someone will teach their students to always have it off because its "slower" or something.

Already happening with Nim.

But I'd expect a system programmer to know better. When to use release and when to use development mode.

Case in point, rust doesn't catch integer overflows in release build either. Maybe Efficiency is more important than we think?

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

#112

Earlier quoted context omitted.

No problem! You might find this website useful, too, as it is filled with resources: https://www.adaic.org/learn/materials/

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, too. I know that GNAT code is heavily documented.

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

#113

Earlier quoted context omitted.

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

I don't understand why there aren't any solutions like this. Why aren't there any languages with a good garbage collector but also let you turn it off and work with memory manually. Maybe there is and I don't know of it? Maybe garbage collected languages and manual memory languages require different design? I don't know.

My favorite example of this is probably Clean, which solves this using uniqueness types. You can have unique values that are managed using compile time memory management, but also are free to use garbage collected values.

It makes for a very performant pure functional language. My only gripe is that the stdlib is abysmal, and hardcoded into the compiler.

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

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

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.

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

#115
post #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'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.

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

#116

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.

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 easie…

Except that level of security I could already do with Pascal dialects like Turbo Pascal or Modula-2, hence why I really don't see much value in Zig, other than being more appealing to younger generations.

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

#117

Earlier quoted context omitted.

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

I don't understand why there aren't any solutions like this. Why aren't there any languages with a good garbage collector but also let you turn it off and work with memory manually. Maybe there is and I don't know of it? Maybe garbage collected languages and manual memory languages require different design? I don't know.

You can use value types and do manual memory management in C#, F#, VB, D, Nim, Swift (RC is GC).

Then on the past languages that failed to gain steam, Mesa/Cedar, Modula-2+, Modula-3, Oberon, Oberon-07, Oberon-2, Active Oberon, Eiffel, Sing#, System C#, Component Pascal, among many others.

The only thing missing is that many are badly taught, use new everywhere and don't bother to fully understand all language features.

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

#118
post #49

Earlier quoted context omitted.

I don't understand why there aren't any solutions like this. Why aren't there any languages with a good garbage collector but also let you turn it off and work with memory manually. Maybe there is and I don't know of it? Maybe garbage collected languages and manual memory languages require different design? I don't know.

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

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

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

Zig uses @something everywhere, maybe they just like Objective-C.

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

#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 without loss of information, and since the only typed expression is the @as, every other integer in the line will coerce to its type.

Post reply on HN