We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
Now that #733 landed, T to *const T is less important. One might even make the case that such a cast is dangerous.
T
*const T
The text was updated successfully, but these errors were encountered:
here are some examples I threw together of how this implicit cast can bite you in other parts of code:
const Contents = struct{ value: u32, }; const Container = struct{ contents: Contents, }; test "LHS: value, RHS: value" { var container = Container{ .contents = Contents{ .value = 1 } }; var x: Contents = undefined; x = container.contents; container.contents.value = 2; std.debug.assert(x.value == 1); } test "LHS: pointer, RHS: value" { var container = Container{ .contents = Contents{ .value = 1 } }; var x: *const Contents = undefined; // this is the only line that has changed! x = container.contents; // this line now has very different meaning container.contents.value = 2; std.debug.assert(x.value == 2); } ///////////// fn returnCopy() Contents { var container = Container{ .contents = Contents{ .value = 1 } }; return container.contents; // returning copy } fn returnPointer() *const Contents { var container = Container{ .contents = Contents{ .value = 1 } }; return container.contents; // looks the same, but very different meaning! } fn anotherFunction() void { var container = Container{ .contents = Contents{ .value = 999 } }; } test "function returning copy" { const contents = returnCopy(); anotherFunction(); std.debug.assert(contents.value == 1); } test "function returning pointer" { const contents = returnPointer(); anotherFunction(); std.debug.warn("{}\n", contents.value); // this is garbage }
Sorry, something went wrong.
d5648d2
remove 3 more implicit casts to const pointers
eb0b1d3
see #1465
No branches or pull requests
Now that #733 landed,
T
to*const T
is less important. One might even make the case that such a cast is dangerous.The text was updated successfully, but these errors were encountered: