-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Discussion: Array/Slice typechecking #568
Comments
var varArrayConst = []const i64 { 1, 2, 3 }; This should be "error: const qualifier invalid on array literal". If you think about it, the memory of the array is contained within the For a slice, the initial The way to think about this stuff is with pointers. When you have a pointer, one of the properties of a pointer is whether it is const or not. A slice is a pointer and a length, and the const-ness of the slice is exactly the const-ness of the pointer. When you declare a local variable, the memory for that local variable is on the stack. If you take a pointer to that local variable, the const-ness of the pointer is whether or not the local variable is
Zig won't implicitly take a mutable reference to something, but it will implicitly take a const reference to something. I'll make the fixes and update the docs before closing this issue, and if you have any other questions, I'll try to answer them. |
if
Ok, so does const pointers then work the same as const slices (Implicit cast from value to pointer)?
It does! Ok, so it all is consistent and I just misunderstood. Thx for clearing it all up. |
I appreciate you filing this issue. These are the little details I want to get right. I'll be sure to add the missing error and clear up the docs before closing the issue. |
One other quick thing, which I should probably have mentioned as it was one of the sources of confusion. Arrays/Slices in arrays, which in initializers are kinda weird because of inferred array size.
In type signatures i never have to guess as to whether [] is an inferred array or a slice.
|
Currently Proposal: change array initialization syntax to |
@Hejsil is it fair to say that there are open issues to address all of your discussion points in this issue? |
@andrewrk If there is an issue for having inferred size of array be |
One of the best simple changes Zig gives us over C is the ability to have actual arrays/slices (not just pointers) in our code. However, after playing with these arrays, I found that the type checking did not make sense to me. I hope that this issue will either:
Ok, so let me explain why I have a problem understanding the type system behind arrays and slices. First, the documentation doesn't seem to explain the use of
const
in types such as pointers and slices. This means, I kinda have to go by what I think it means, which is:[]const i64
means a slice, whose elements cannot be changed ora slice of const i64
.With this in mind, let's go use arrays and slices.
Ok, so far so good. I seems I'm not totally misunderstanding it since
sliceOfConstElements[0] = 1;
gave an error. I wonder why we can't have arrays of constant elements though...Next, we ofcourse want to be able to assign our variables to values, instead of undefined:
Ooh, so I can't assign a slice to an array literal? But I can if the slice is const? Why? My best guess is, that apparently
[4]i64
actually implicitly is const. Why not call the type[4]const i64
?Well, ok. Let's test something then. Can I declare const in the literal?
I can? Ooook? So what is the difference between
[]const i64{ 1, 2, 3, 4 }
and[]i64{ 1, 2, 3, 4 }
?No difference? So I guess the compiler does not respect
const
in array literals.I could go on with my story format like this, but I think you all get that this is confusing me a little. Here is a bigger piece of code, that outlines more things that confuse me:
The text was updated successfully, but these errors were encountered: