Skip to content

Unable to specify a type has to be const #6897

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

Closed
BarabasGitHub opened this issue Oct 31, 2020 · 2 comments
Closed

Unable to specify a type has to be const #6897

BarabasGitHub opened this issue Oct 31, 2020 · 2 comments
Labels
proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Milestone

Comments

@BarabasGitHub
Copy link
Contributor

BarabasGitHub commented Oct 31, 2020

I mainly run into this when using a function to create a struct. A simple example would be a custom slice (currently my case is for a 2d view on memory).

fn slice(dataType: Type) struct {
    return struct {
        data: []DataType,
        // more amazing stuff
    }
}

Sometimes I want to have a slice which can be used to edit the data that it points to, sometimes I want it to point to read only data, which shouldn't/can't be modified. I would like to be able to do something like:

read_write = slice(f32).init(bla);
read_only = slice(const f32).init(bla);

but the read_only line will fail to compile. I don't see any way to get a const type currently.

@BarabasGitHub
Copy link
Contributor Author

I guess for now the workaround is to use the slice or pointer type as input instead of the pointed to type.
So it'll become:

fn slice(SliceDataType: Type) struct {
    assertTypeIsSlice(SliceDataType);
    return struct {
        data: SliceDataType,
        // more amazing stuff
    }
}

@andrewrk andrewrk added the proposal This issue suggests modifications. If it also has the "accepted" label then it is planned. label Nov 1, 2020
@andrewrk andrewrk added this to the 0.8.0 milestone Nov 1, 2020
@thejoshwolfe
Copy link
Contributor

Yeah, that's the workaround. You can put some comptime asserts in there to make sure you're getting a type that fits your constraints. The idea of putting type constraints in the language itself is discussed in another issue (or perhaps many other issues).

Regarding the title of this issue "Unable to specify a type has to be const", this is intentional.

Zig's philosophy differs from C's in this regard. In Zig, an integer can't be const on its own. Instead, a memory region can be const in a certain context. In the language this means that pointers to memory can be const pointers (and slices are a kind of pointer). This design decision stemmed from several observations about nonsensical const placements that are allowed in C's type system, such as declaring a function to return a const int. An integer can be copied by value, so it makes no sense to declare an integer as const, but you may wish to specify its declared storage location const, such as a local variable (or rather a local const) or a global const. In those cases in Zig, the word const is not applied to the type but rather to the declaration itself.

I hope that makes sense.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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

3 participants