-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Namespace of pub const
in structs is not obvious
#8522
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
Comments
If I did, |
const sample = "one";
const duplicate = "one";
const Struct = struct {
const duplicate = "two";
pub fn something() void {
_ = sample; // sample is "one". this is accessible even though it's in the outer scope because it's a comptime value
_ = duplicate; // what is duplicate here? is it "one" or "two"? zig makes this a shadowing error instead
}
};
test "" {_ = Struct.something;} |
I don't want to use the outer one from inside, but I would like to use the inside from the outside, which I can do here: const duplicate = "one";
const Struct = struct {
duplicate: []const u8 = "two",
};
test "" {
const x = Struct{ .duplicate = "three" };
_ = duplicate;
_ = x.duplicate;
} If the above works for const duplicate = "one";
const Struct = struct {
duplicate: []const u8 = "two",
parse: fn () void = undefined,
};
test "" {
const x = Struct{
.duplicate = "three",
.parse = fn () void{},
};
_ = duplicate;
_ = x.duplicate;
}
This probably should have been my initial issue/query ... I guess I got sidetracked since it seems that the |
I see now that I raised / labelled this issue based on a misunderstanding - there is nothing specific to fn() types. This also fails to compile, because the const expect = @import("std").testing.expect;
// Structs can have global declarations.
// Structs can have 0 fields.
const Empty = struct {
pub const PI = 3.14;
};
pub const PI = 3.14;
test "struct namespaced variable" {
expect(Empty.PI == 3.14);
expect(@sizeOf(Empty) == 0);
// you can still instantiate an empty struct
const does_nothing = Empty{};
}
What is the real namespace of const expect = @import("std").testing.expect;
pub const Empty = struct {
pub const PI = 3.14;
};
pub const Empty2 = struct {
pub const PI = 3.14;
};
test "struct namespaced variable" {
expect(Empty.PI == 3.14);
expect(Empty2.PI == 3.14);
} What is the benefit of Zig having this File/Struct namespace constraint? Something in one namespace affecting a parent namespace fails to "Communicate intent precisely" to me. Here is another odd example: const expect = @import("std").testing.expect;
pub const Empty = struct {
pub const PI = 3.14;
};
pub const Empty2 = struct {
child: Empty = Empty{},
pub const PI = 3.14;
};
test "struct namespaced variable" {
const e2 = Empty2{};
expect(Empty.PI == 3.14);
expect(e2.child.PI == 3.14);
expect(Empty2.PI == 3.14);
}
I think namespacing still needs a few deep thoughs. On a design note, why is pub const Point3D = struct {
x: 0,
y: 0,
const z: 0, // stay on the ground please
}; |
pub const
in structs is not obvious
Shadowing errors in zig are about scope, not namespaces. const Sample = struct {
const PI = 3.14;
}; Here, Inside the const Sample = struct {
const PI = 3.14;
// Sample is in scope, so you can use Sample.PI
// PI is a variable that is in scope, so you can use PI directly
};
// Sample is in scope, so you can use Sample.PI
// PI is not in scope, you must use Sample.PI to access it Note that
This produces the same error: expect(e2.PI == 3.14); You can only access struct declarations from the struct type, not from an instance of that struct const Sample = struct {
field: u8,
const Decl = "hi";
};
test "" {
_ = Sample.Decl; // OK
_ = Sample.field; // Not OK
const sample = Sample{.field = 10};
_ = sample.field; // OK
_ = sample.Decl // Not OK
_ = @TypeOf(sample).Decl // OK
} There is currently an exception here for function declarations (eg
The ability to have private fields was removed in #2059, I don't think a clear reason was given. |
This was very illuminating:
You can only access struct declarations from the struct type, not
from an instance of that struct
Your sentence should go straight into the documentation. It clears up a
few of the secondary issues I was running into.
And I'm clear now on my main misunderstanding, due to to your reference
to shadowing. I had read about that but didn't connect it to the error
messages I was seeing. Something that will improve with time as
documentation and compiler messages evolve.
Thanks all for the assist.
--
Mark Lawrence
|
I would have expected the
parse
member ofInterface
below to be not in the top-level namespace, like otherstruct { pub const X...
members are:Version 0.7.1 on Linux.
The text was updated successfully, but these errors were encountered: