logoalt Hacker News

pton_xdlast Sunday at 4:18 PM3 repliesview on HN

"Zig doesn’t have lambdas"

This surprises me (as a C++ guy). I use lambdas everywhere. What's the standard way of say defining a comparator when sorting an array in Zig?


Replies

tapirllast Sunday at 4:54 PM

Normal function declarations.

This is indeed a point which makes Zig inflexible.

show 1 reply
jmulllast Sunday at 5:23 PM

You can declare an anonymous struct that has a function and reference that function inline (if you want).

There's a little more syntax than a dedicated language feature, but not a lot more.

What's "missing" in zig that lambda implementations normally have is capturing. In zig that's typically accomplished with a context parameter, again typically a struct.

show 1 reply
tux1968last Sunday at 4:36 PM

Same as C, define a named function, and pass a pointer to the sorting function.

show 2 replies