Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

slightly different array literal syntax to distinguish from slices #1797

Closed
andrewrk opened this issue Nov 27, 2018 · 7 comments
Closed

slightly different array literal syntax to distinguish from slices #1797

andrewrk opened this issue Nov 27, 2018 · 7 comments
Labels
accepted This proposal is planned. breaking Implementing this issue could cause existing code to no longer compile or have different behavior. proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Milestone

Comments

@andrewrk
Copy link
Member

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};
@andrewrk andrewrk added the proposal This issue suggests modifications. If it also has the "accepted" label then it is planned. label Nov 27, 2018
@andrewrk andrewrk added this to the 0.5.0 milestone Nov 27, 2018
@tgschultz
Copy link
Contributor

Or we could use a builtin:

var hi = "hi";
const not_a_mystery = @arrayInit([]u8, &hi); //[1][]u8

@raulgrell
Copy link
Contributor

A few ideas:

var hi = "hi";
const dotdot = [..][]u8{&hi};
const prefixplusplus = ++[][]u8{&hi};
const zero =[0][]u8{&hi};

@hryx
Copy link
Contributor

hryx commented Nov 28, 2018

I'll admit this has bitten me a lot. Love the underscore idea; when I see the _ character I know it means "some value goes here, but I don't really care what it is". And no syntactical conflicts inside the brackets. Builtin is clear, but a bit verbose for such a rudimentary/common operation, in my opinion.

Edit: @raulgrell the .. is nice too, but that might be confusing too because it means the opposite when converting an array into a slice: var slice = array[0..];

@raulgrell
Copy link
Contributor

raulgrell commented Nov 28, 2018

@hryx Yeah, that's one of it's greatest weaknesses but also its greatest strength:

When slicing, IT occurs on the right of a name, it takes an index and it and refers to a value
When declaring/instantiating, occurs on the left of a name, takes no index, and refers to a type

It's similar to the symmetry with declaring a pointer type with a prefix * and dereferencing it with a postfix .*

@andrewrk andrewrk added the accepted This proposal is planned. label Nov 28, 2018
@Hejsil
Copy link
Contributor

Hejsil commented Nov 28, 2018

Related: #568

@andrewrk
Copy link
Member Author

Ah, I was looking for that, thanks @Hejsil. Looks like @thejoshwolfe made an identical proposal to this one in a comment on that issue.

@andrewrk
Copy link
Member Author

andrewrk commented Jun 9, 2019

I need this for #2602 so I'm going to do this in master branch right now.

@andrewrk andrewrk added the breaking Implementing this issue could cause existing code to no longer compile or have different behavior. label Jun 9, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepted This proposal is planned. breaking Implementing this issue could cause existing code to no longer compile or have different behavior. proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Projects
None yet
Development

No branches or pull requests

5 participants