digitalmars.D - const AA require
- Elronnd (5/5) Jul 29 2021 const(int)[int] d;
- Tejas (3/8) Aug 04 2021 Casting ```const``` away is undefined behaviour in D. And what is
- jfondren (18/27) Aug 04 2021 what's wanted here is an assign-once hash table, I imagine.
- Tejas (14/42) Aug 04 2021 Well, OP will have to create non-const AA first and then cast it
- Mathias LANG (4/9) Aug 04 2021 You aren't missing anything. It's a bug, and should be fixed, but
const(int)[int] d; d.require(5, 7); //compile error From a semantics perspective, though, I don't think there's anything wrong with this, and 'require' could reasonably cast the const away before assigning. Am I missing anything?
Jul 29 2021
On Thursday, 29 July 2021 at 20:44:35 UTC, Elronnd wrote:const(int)[int] d; d.require(5, 7); //compile error From a semantics perspective, though, I don't think there's anything wrong with this, and 'require' could reasonably cast the const away before assigning. Am I missing anything?Casting ```const``` away is undefined behaviour in D. And what is this "require"? Maybe show more code?
Aug 04 2021
On Wednesday, 4 August 2021 at 08:08:35 UTC, Tejas wrote:On Thursday, 29 July 2021 at 20:44:35 UTC, Elronnd wrote:what's wanted here is an assign-once hash table, I imagine. require is https://dlang.org/phobos/object.html#.require , and it'd be the assignment line of that code that would have to cast away const. ```d unittest { int[int] nums; nums.require(0, 0); nums[0]++; assert(nums == [0: 1]); } unittest { const(int)[int] nums; assert(__traits(compiles, nums.require(0, 0))); assert(!__traits(compiles, nums[0]++)); } ```const(int)[int] d; d.require(5, 7); //compile error From a semantics perspective, though, I don't think there's anything wrong with this, and 'require' could reasonably cast the const away before assigning. Am I missing anything?Casting ```const``` away is undefined behaviour in D. And what is this "require"? Maybe show more code?
Aug 04 2021
On Wednesday, 4 August 2021 at 09:43:57 UTC, jfondren wrote:On Wednesday, 4 August 2021 at 08:08:35 UTC, Tejas wrote:Well, OP will have to create non-const AA first and then cast it to const, it seems. ```d import std; void main() { int[int] a; a.require(5,7); auto b = cast(const(int)[int])a; } ``` Probably hide the process behind a function to make it look more smooth; otherwise I'm outta ideas.On Thursday, 29 July 2021 at 20:44:35 UTC, Elronnd wrote:what's wanted here is an assign-once hash table, I imagine. require is https://dlang.org/phobos/object.html#.require , and it'd be the assignment line of that code that would have to cast away const. ```d unittest { int[int] nums; nums.require(0, 0); nums[0]++; assert(nums == [0: 1]); } unittest { const(int)[int] nums; assert(__traits(compiles, nums.require(0, 0))); assert(!__traits(compiles, nums[0]++)); } ```const(int)[int] d; d.require(5, 7); //compile error From a semantics perspective, though, I don't think there's anything wrong with this, and 'require' could reasonably cast the const away before assigning. Am I missing anything?Casting ```const``` away is undefined behaviour in D. And what is this "require"? Maybe show more code?
Aug 04 2021
On Thursday, 29 July 2021 at 20:44:35 UTC, Elronnd wrote:const(int)[int] d; d.require(5, 7); //compile error From a semantics perspective, though, I don't think there's anything wrong with this, and 'require' could reasonably cast the const away before assigning. Am I missing anything?You aren't missing anything. It's a bug, and should be fixed, but that'll require making the frontend understand what `require` is (or providing a way to express this in a generic manner).
Aug 04 2021