Thread overview | |||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
October 12, 2014 Really in need of help with std.container.array.d | ||||
---|---|---|---|---|
| ||||
I'm trying to figure out how to add complete support for constness in std.container.array.Array at https://github.com/D-Programming-Language/phobos/commit/703305f0bfb1cc22eff3e44e351cc3db3e03f94f#commitcomment-8114056 My current solution (which currently fails) at https://github.com/nordlow/phobos/commit/5c57cb18c2b9d340a19d19207deca5af0339cf7e#diff-0 tries to solve the problem by making wrapper struct Range a template that captures the constness of parenting Array. My current implementation of array.d at https://github.com/nordlow/phobos/blob/inout-array-range/std/container/array.d#L221 however fails its unittests as array.d(223,19): Error: variable std.container.array.Array!int.Array.Range!(inout(Array!int)).Range._outer only parameters or stack based variables can be inout array.d(430,5): Error: template instance std.container.array.Array!int.Array.Range!(inout(Array!int)) error instantiating array.d(833,5): instantiated from here: Array!int array.d(862,5): Error: static assert (!true) is false and I have no clue how to proceed with this. Please help, me and at least one other D developer is hindered by this. |
October 12, 2014 Re: Really in need of help with std.container.array.d | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nordlöw | On Sunday, 12 October 2014 at 20:17:38 UTC, Nordlöw wrote: https://github.com/nordlow/phobos/commit/5c57cb18c2b9d340a19d19207deca5af0339cf7e#diff-0 > Made some corrections https://github.com/nordlow/phobos/compare/inout-array-range but gives a similar error array.d(223,19): Error: variable std.container.array.Array!int.Array.Range!(inout(Array!int)).Range._outer only parameters or stack based variables can be inout array.d(430,5): Error: template instance std.container.array.Array!int.Array.Range!(inout(Array!int)) error instantiating array.d(833,5): instantiated from here: Array!int array.d(816,5): Error: template instance std.container.array.Array!int.Array.Range!(Array!int) error instantiating array.d(833,5): instantiated from here: Array!int array.d(862,5): Error: static assert (!true) is false |
October 13, 2014 Re: Really in need of help with std.container.array.d | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nordlöw | hm, the problems seams to be that "inout Array" is not becoming "const Array" and friends. A blunt force solution would be to create a range as Range!(ReturnType!Array...)(cast(Array!T)this, low, high); and then do the correct casts in Range. The ReturnType would be the ReturnType of opIndex of the Range type and the Array would be stored as plain array without any qualifier. other than that I'm not sure how to go about this. |
October 14, 2014 Re: Really in need of help with std.container.array.d | ||||
---|---|---|---|---|
| ||||
Posted in reply to Robert burner Schadek | On Monday, 13 October 2014 at 13:46:56 UTC, Robert burner Schadek wrote: > other than that I'm not sure how to go about this. I tried replace inout with a C++-style member duplication at https://github.com/nordlow/phobos/commit/b2a4ca28bf25bf7c6149566d066cbb54118b36b4 Now it instead errors as Error: mutable method std.container.array.Array!int.Array.__fieldPostBlit is not callable using a const object array.d(252,26): Error: cast(inout(int))this._outer.opIndex(this._a) is not an lvalue array.d(258,26): Error: cast(inout(int))this._outer.opIndex(this._b - 1LU) is not an lvalue ../../std/algorithm.d(2364,9): Error: cannot modify const expression result array.d(276,24): Error: template instance std.algorithm.move!(const(int)) error instantiating array.d(439,5): instantiated from here: Range!(const(Array!int)) array.d(851,5): instantiated from here: Array!int array.d(294,26): Error: cast(inout(int))this._outer.opIndex(this._a + i) is not an lvalue Error: mutable method std.container.array.Array!int.Array.~this is not callable using a const object array.d(320,30): Error: None of the overloads of 'opSliceAssign' are callable using a const object, candidates are: array.d(507,10): std.container.array.Array!int.Array.opSliceAssign(int value) array.d(514,10): std.container.array.Array!int.Array.opSliceAssign(int value, ulong i, ulong j) array.d(326,38): Error: None of the overloads of 'opSliceAssign' are callable using a const object, candidates are: array.d(507,10): std.container.array.Array!int.Array.opSliceAssign(int value) array.d(514,10): std.container.array.Array!int.Array.opSliceAssign(int value, ulong i, ulong j) array.d(299,32): Error: mutable method std.container.array.Array!int.Array.Range!(Array!int).Range.this is not callable using a const object array.d(309,32): Error: mutable method std.container.array.Array!int.Array.Range!(Array!int).Range.this is not callable using a const object array.d(443,5): Error: template instance std.container.array.Array!int.Array.Range!(Array!int) error instantiating array.d(851,5): instantiated from here: Array!int ../../std/typecons.d(3889,17): Error: template instance object.destroy!(Payload) error instantiating array.d(169,26): instantiated from here: RefCounted!(Payload, cast(RefCountedAutoInitialize)0) array.d(851,5): instantiated from here: Array!int array.d(880,5): Error: static assert (!true) is false Ideas? |
October 14, 2014 Re: Really in need of help with std.container.array.d | ||||
---|---|---|---|---|
| ||||
Posted in reply to Robert burner Schadek | On Monday, 13 October 2014 at 13:46:56 UTC, Robert burner Schadek wrote: > A blunt force solution would be to create a range as > Range!(ReturnType!Array...)(cast(Array!T)this, low, high); > and then do the correct casts in Range. > The ReturnType would be the ReturnType of opIndex of the Range type and the Array would be stored as plain array without any Do you mean a mutable array containing references to inout data? > qualifier. Could you please give me a code example? I'm not skilled enough in D to follow this description. |
October 14, 2014 Re: Really in need of help with std.container.array.d | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nordlöw | On Tuesday, 14 October 2014 at 12:51:29 UTC, Nordlöw wrote: > > Could you please give me a code example? I'm not skilled enough in D to follow this description. struct Range(T) { Array!S array; T opIndex(size_t i) { return cast(T)array[i]; } } struct Array(T) { Range!(const(T)) opSlice() const { return Range!(const(T))(cast(Array!T)array, 0, length); } Range!(T) opSlice() const { return Range!(T)(cast(Array!T)array, 0, length); } } |
October 15, 2014 Re: Really in need of help with std.container.array.d | ||||
---|---|---|---|---|
| ||||
Posted in reply to Robert burner Schadek | On Tuesday, 14 October 2014 at 16:08:31 UTC, Robert burner Schadek wrote: > On Tuesday, 14 October 2014 at 12:51:29 UTC, Nordlöw wrote: > >> >> Could you please give me a code example? I'm not skilled enough in D to follow this description. > > struct Range(T) { > Array!S array; > T opIndex(size_t i) { > return cast(T)array[i]; > } > } > > struct Array(T) { > Range!(const(T)) opSlice() const { > return Range!(const(T))(cast(Array!T)array, 0, length); > } > > Range!(T) opSlice() const { > return Range!(T)(cast(Array!T)array, 0, length); > } > } I tried that at https://github.com/nordlow/phobos/commit/9daf235d7091f76cd941e29e3c167d559bf56a94 but that triggers a new interesting suite of errors Error: mutable method std.container.array.Array!int.Array.__fieldPostBlit is not callable using a const object array.d(252,26): Error: cast(inout(int))this._outer.opIndex(this._a) is not an lvalue array.d(258,26): Error: cast(inout(int))this._outer.opIndex(this._b - 1LU) is not an lvalue ../../std/algorithm.d(2364,9): Error: cannot modify const expression result array.d(276,24): Error: template instance std.algorithm.move!(const(int)) error instantiating array.d(444,5): instantiated from here: Range!(const(Array!int)) array.d(856,5): instantiated from here: Array!int array.d(299,26): Error: cast(int)this._outer.opIndex(this._a + i) is not an lvalue Error: mutable method std.container.array.Array!int.Array.~this is not callable using a const object array.d(325,30): Error: None of the overloads of 'opSliceAssign' are callable using a const object, candidates are: array.d(512,10): std.container.array.Array!int.Array.opSliceAssign(int value) array.d(519,10): std.container.array.Array!int.Array.opSliceAssign(int value, ulong i, ulong j) array.d(331,38): Error: None of the overloads of 'opSliceAssign' are callable using a const object, candidates are: array.d(512,10): std.container.array.Array!int.Array.opSliceAssign(int value) array.d(519,10): std.container.array.Array!int.Array.opSliceAssign(int value, ulong i, ulong j) array.d(304,32): Error: mutable method std.container.array.Array!int.Array.Range!(Array!int).Range.this is not callable using a const object array.d(314,32): Error: mutable method std.container.array.Array!int.Array.Range!(Array!int).Range.this is not callable using a const object array.d(448,5): Error: template instance std.container.array.Array!int.Array.Range!(Array!int) error instantiating array.d(856,5): instantiated from here: Array!int ../../std/typecons.d(3889,17): Error: template instance object.destroy!(Payload) error instantiating array.d(169,26): instantiated from here: RefCounted!(Payload, cast(RefCountedAutoInitialize)0) array.d(856,5): instantiated from here: Array!int array.d(885,5): Error: static assert (!true) is false Comint exited abnormally with code 1 at Wed Oct 15 23:14:37 I'm stuck. Need help. |
October 16, 2014 Re: Really in need of help with std.container.array.d | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nordlöw | On Wednesday, 15 October 2014 at 21:15:14 UTC, Nordlöw wrote:
>
> Comint exited abnormally with code 1 at Wed Oct 15 23:14:37
>
> I'm stuck. Need help.
I will give it a try
|
October 16, 2014 Re: Really in need of help with std.container.array.d | ||||
---|---|---|---|---|
| ||||
Posted in reply to Robert burner Schadek | On Thursday, 16 October 2014 at 13:56:18 UTC, Robert burner Schadek wrote:
>> I'm stuck. Need help.
>
> I will give it a try
Thank you.
|
October 19, 2014 Re: Really in need of help with std.container.array.d | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nordlöw | On Wednesday, 15 October 2014 at 21:15:14 UTC, Nordlöw wrote: > https://github.com/nordlow/phobos/commit/9daf235d7091f76cd941e29e3c167d559bf56a94 > > but that triggers a new interesting suite of errors > > Error: mutable method std.container.array.Array!int.Array.__fieldPostBlit is not callable using a const object [...] > Error: mutable method std.container.array.Array!int.Array.~this is not callable using a const object [...] > I'm stuck. Need help. I reduced it to this: ---- struct RefCounted { this(this) /* const doesn't help */ {} ~this() /* const doesn't help */ {} } struct Array { RefCounted _data; } void main() {const Array a; const copy = a;} /* works */ struct RangeM {Array a;} /* works */ struct RangeC {const Array a;} /* error */ ---- Looks like a compiler bug to me. And here's a workaround: ---- struct RangeC { const Array[1] a_; @property ref const(Array) a() {return a_[0];} } ---- Using it in std.container.array: ---- static struct Range(A) { private A[1] _outer_; private @property ref const(A) _outer() const {return _outer_[0];} private @property ref A _outer() {return _outer_[0];} /* ... */ private this(ref A data, size_t a, size_t b) { version(none) _outer = data; /* "Error: mutable method [...].Array.opAssign is not callable using a const object" */ else version(none) _outer_[0] = data; /* Errors about postblit. */ else _outer_ = data; /* works */ /* ... */ } ---- Then you also have to disable any methods/overloads that would return mutable data when A isn't mutable, e.g.: ---- static if (isMutable!A) @property ref T front() {/* ... */} ---- Related: `front` and `back` cannot be inout, because the constness depends not on Range's constness, but on Array's. |
Copyright © 1999-2021 by the D Language Foundation