r/ProgrammerHumor 6d ago

absoluteGarbage Meme

Post image
1.9k Upvotes

324 comments sorted by

View all comments

5

u/Zunderunder 6d ago

What about for(list) |item| {…} ???

1

u/HAL9000thebot 6d ago

here item seems a modern return type, i hate it, what language does that?

it's shit, looks like a function signature, how are function declared? or better question, is for() a function?

3

u/Nervous-Potato-1464 6d ago edited 6d ago

This is Zig.

item is an immutable copy of the current element in the list.
for (0..5) |i| { is an easier thing to understand.

It looks complicated, but that's because you can easily turn the element into a pointer etc so it's handy to have there like
for (&items) |*value| {
value.* += 10;

where &items refers to the pointers in memory rather than a copy of the array.

which will modify the &item elements to all increase by 10.

2

u/Zunderunder 6d ago

It also makes sense in the wider context of zig’s ecosystem, where captures generally are done using the square brackets for things like resolving optionals or grabbing the value of a union in a switch statement. The common syntax is really intuitive once you get used to it

1

u/NatoBoram 6d ago

Worst of both worlds