Skip to content

slightly different array literal syntax to distinguish from slices #1797

Closed
@andrewrk

Description

@andrewrk

Guess what @typeOf(mystery) is:

var hi = "hi";
const mystery = [][]u8{&hi};

It's [1][]u8. It looks like a double slice, but it's actually an array literal with an inferred number of items. It is equivalent to:

var hi = "hi";
const not_a_mystery = [1][]u8{&hi};

This is important because arrays usually can not be used where slices are expected, and it starts to get confusing with const, especially when you consider that there is a missing compile error for this:

const why_isnt_this_an_error = []const []const{"hi"};

The const on the array literal is nonsense and should be a compile error.

It's nice to infer the number of array items, but it shouldn't look like a slice. How about this syntax instead?

var hi = "hi";
const not_a_mystery = [_][]u8{&hi};

Metadata

Metadata

Assignees

No one assigned

    Labels

    acceptedThis proposal is planned.breakingImplementing this issue could cause existing code to no longer compile or have different behavior.proposalThis issue suggests modifications. If it also has the "accepted" label then it is planned.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions