-
-
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
make aggregate types non-copyable by default; provide copyable attribute #3804
Comments
I don't think enums should be, what about something like: const Foo = struct {
const Self = @This();
x: i32,
qux: enum {
a, b, c,
pub fn validate(thisenum: *@This()) bool {
const self = @fieldParentPtr(Self, "qux", thisenum);
return switch (thisenum) {
a => self.x < 42,
b => self.x < 100,
c => self.x >= 50,
};
}
},
}; |
As good as it sounds, this proposal implies explicit move- and copy-semantics for a lot of types and special care for a lot of generics: Lines 139 to 142 in 85e1e3b
This would not work with non- And as @daurnimator said: |
isn't this basically the same thing as #3803 but with pinned types rather than fields? perhaps these can be unified by having a const Point2 = @Pin(struct {
x: i32,
y: i32,
}); and #3803 becomes: const FixedBufferAllocator = struct {
allocator: @Pin(Allocator),
buf: []u8,
}; in cases that it doesn't make sense for the type to be movable such as function pointers |
Perhaps there should be a special copy operator |
Closing this in favor of #7769. As that issue points out, copy prevention is only tangentially related to RAII, and doesn't really do a good job of replacing it. Once those use cases are removed, it seems silly to disallow copies on most structs. |
This is a competing proposal with #3803. It solves the same problem in a different way, which is arguably simpler, and safer by default.
It's pretty easy to explain: make all aggregate types non-copyable by default. So this would be an error:
But this would work:
Some notes:
return
statement #2765 implemented.The text was updated successfully, but these errors were encountered: