Stream: compiler development

Topic: List and Slice in struct?


view this post on Zulip Anton (Mar 11 2025 at 12:42):

In src/build/specialize_types/IR.zig it's very common to see a List, Slice, NonEmptySlice,... inside the struct. Is this a zig convention?

/// A definition, e.g. `x = foo`
pub const Def = struct {
    pattern: Pattern.Idx,
    /// Named variables in the pattern, e.g. `a` in `Ok a ->`
    pattern_vars: TypedIdent.Slice,
    expr: Expr.Idx,
    expr_type: Type.Idx,
    /// todo
    pub const List = collections.SafeMultiList(@This());
    /// todo
    pub const Slice = List.Slice;
};

view this post on Zulip Brendan Hansknecht (Mar 11 2025 at 15:41):

Yeah, it's for type safety and ease of use. I have definitely seen it in other zig code. Especially common for DOD types that will only ever be in lists. It's the basic way to make associated types


Last updated: Jul 06 2025 at 12:14 UTC