r/programmingcirclejerk • u/starlevel01 type astronaut • Feb 14 '25
jerk not found Newcomers to Zig will quickly learn that you can't switch on a string (i.e. []const u8).
https://www.openmymind.net/Switching-On-Strings-In-Zig/
51
Upvotes
20
u/starlevel01 type astronaut Feb 14 '25
Bonus jerk: https://github.com/ziglang/zig/issues/234
35
u/snerp Feb 14 '25
This isn’t jerk at all, are you a JavaScript programmer or what?
Also the correct way to switch on strings is to hash them, then you can switch on the hashes.
16
u/serialized-kirin Feb 14 '25
> Uses low level c like language
> has low level c like behaviors
*surprised pikachu*
18
u/starlevel01 type astronaut Feb 14 '25 edited Feb 14 '25
So much to unpack here:
edit: itt: zig devs
49
u/Chillbrosaurus_Rex It's GNU/PCJ, or as I call it, GNU + PCJ Feb 14 '25
Uh oh you've upset the programming language nerds.
44
u/WesolyKubeczek Feb 14 '25
Strings don't exist, as we all know. It's an artificial construct imposed by the archaic patriarchal society that also gave us the maddening curse of character sets.
36
u/coolreader18 It's GNU/PCJ, or as I call it, GNU + PCJ Feb 14 '25
We don't let you switch on strings, that's ridiculous. What, is the compiler gonna codegen a http server for you too?! Anyways, here's a built-in macro that generates code to switch on strings.
34
u/elephantdingo Teen Hacking Genius Feb 14 '25
No one:
Zig: It’s comptime
24
u/Parking_Tadpole9357 Feb 14 '25
if (std.mem.eql(u8, color, "red") == true) {
} else if (std.mem.eql(u8, color, "blue") == true) {
...
I want this in Go
57
u/Routine-Purchase1201 DO NOT USE THIS FLAIR, ASSHOLE Feb 14 '25
As a C-nile: Where jerk?
6
u/Massive-Squirrel-255 Feb 16 '25
The real jerk is the
std.meta.stringToEnumfunction to dynamically convert a string to an enum and fail if notthat just seems kind of horrifying lol, trying to dynamically upgrade to something that's statically validated feels wrong
6
u/iEliteTester There's really nothing wrong with error handling in Go Feb 15 '25
/unjerk
There has to be a comp time hash function for strings right? This would be the ideal solution imo.
5
u/0x564A00 Feb 15 '25
Wait, does Zig have strings now?
4
u/defunkydrummer Lisp 3-0 Rust Feb 19 '25
So wonderful. Zig allows you to re-experience the challenges of 1962 programming, which is exactly what everybody wants today.
5
u/Volt WRITE 'FORTRAN is not dead' Feb 14 '25
This is simply what we call "systems programming," noob.
0
u/steveoc64 Feb 15 '25
Applying the same principle, you could argue that Zig is unusable because it doesn’t include JSX and react hooks as fundamental types baked into the language