#Help explaining anonymous struct literal syntax

1 messages · Page 1 of 1 (latest)

naive mirage
#

std.heap.GeneralPurposeAllocator is a function that returns a type. By adding {} at the end we simply instanciate that type.

#

You may recognize this pattern from arrays where you can write var a = [3]f32{...} which is equivalent to var a: [3]f32 = .{...}

#

The empty {} means that we use the default values for all fields in std.heap.GeneralPurposeAllocator

keen tapir
#

The .{…} syntax inprint is a Tuple, basically an anonymous struct with numerical indices and a .len property

naive mirage
#

Additionally, zig doesn't really like vaArgs and only supports it because of C (I think)

#

So the standard way to do an unknown number of arguments is to pass a tuple and then do some comptime iteration over it

trail delta
#

thanks so much! The {} at the end to instantiate finally makes sense. I've seen that everywhere (including Go)

trail delta
#

Thanks @keen tapir - Zig tuples are now my favorite tuples. Them supporting ++ is great:

   const values = .{
        @as(u32, 1234),
        @as(f64, 12.34),
        true,
        "hi",
    } ++ .{false} ** 2;
keen tapir
#

Yeah, I like that ++ and ** do what you expect them to do zerobeef

trail delta
#

what does ** do?

pliant badge
trail delta
#

nice!

#

how do I mark this post as answered?

pliant badge
#

react with ✅

#

nah like on the post above

trail delta
#

i screwed somethign up and can't react now 😦

ionic hedge
#

Just to add on to that: Zig tuples are designed to generally be fairly interchangable with arrays. ** and ++ are sort of made to be used on arrays (and slices), but Zig tries to make arrays like [3]u32 act similarly to the equivalent tuple, in this case struct { u32, u32, u32 }. (btw, in case you weren't aware, that's the syntax to explicitly declare a tuple type if you ever need to)

Anyways, afaict deleting the original post makes it impossible to react to threads, which is a bit silly... <@&761628228409884672>, could one of y'all manually close this one?

faint bane
#

lmao mobile ui is shit, can't close wheeze

quasi pollen
#

done 🙂

vestal pendant
#

ahh stop chatting after closing