Enum literal vs container level variable in the language reference

I was looking in the language reference for a section which describes why this works, because I was surprised it was acceptable.

const list: ArrayListUnmanaged(i32) = .empty;

I assumed one would instead have to write:

const list = ArrayListUnmanaged(i32).empty;

empty is declared inside the ArrayListUnmanaged struct as:

pub const empty: Self = .{
    .items = &.{},
    .capacity = 0,
};

so it’s a container-level variable.

But the = .empty part of the assignment statement looks like an enum literal. What is the language rule that tells the compiler to interpret that as ArrayListUnmanaged(i32).empty?

It seems the . in this case is used to scope the “empty” identifier to the container, which is a different usage than the . in an enum literal. Is there a name for this type of usage? And is it possible there might be a conflict between the two usages?

“Decl literals” are a recent addition and are in the 0.14.0 release notes.

2 Likes