|
Archives
D Programming
DD.gnu digitalmars.D digitalmars.D.bugs digitalmars.D.dtl digitalmars.D.dwt digitalmars.D.announce digitalmars.D.learn digitalmars.D.debugger C/C++ Programming
c++c++.announce c++.atl c++.beta c++.chat c++.command-line c++.dos c++.dos.16-bits c++.dos.32-bits c++.idde c++.mfc c++.rtl c++.stl c++.stl.hp c++.stl.port c++.stl.sgi c++.stlsoft c++.windows c++.windows.16-bits c++.windows.32-bits c++.wxwindows digitalmars.empire digitalmars.DMDScript |
digitalmars.D.dtl - D struct are difficult to work with!
I found D structs to be harder to work with than C++ ones, for example, with array i can directly modify a struct by array[i].x = 9; but when it is in a Vector, I need to copy it out, modify the copy, and copy back into the Vector. SomeStruct s = vector[i]; s.x = 9; vector[i] = s; Using a pointer is very inconvenient, especially for math structs, I can see things like *result = (*a + *b) * (*c) Would it be nice to extend "inout" to function return, so to have Vector opIndex with the following signature for structs: inout value_type opIndex(index_type index) This helps to make containers more transparent with native array, and much nicer to work with. And extend it to variables: inout SomeStruct s = vector[i]; s.x = 9; Mar 19 2006
Hong Wing wrote:I found D structs to be harder to work with than C++ ones, for example, with array i can directly modify a struct by array[i].x = 9; but when it is in a Vector, I need to copy it out, modify the copy, and copy back into the Vector. SomeStruct s = vector[i]; s.x = 9; vector[i] = s; Using a pointer is very inconvenient, especially for math structs, I can see things like *result = (*a + *b) * (*c) Would it be nice to extend "inout" to function return, so to have Vector opIndex with the following signature for structs: inout value_type opIndex(index_type index) This helps to make containers more transparent with native array, and much nicer to work with. And extend it to variables: inout SomeStruct s = vector[i]; s.x = 9; Mar 19 2006
On Sun, 19 Mar 2006 08:16:26 +0000 (UTC), Hong Wing wrote:I found D structs to be harder to work with than C++ ones, for example, with array i can directly modify a struct by array[i].x = 9; but when it is in a Vector, I need to copy it out, modify the copy, and copy back into the Vector. SomeStruct s = vector[i]; s.x = 9; vector[i] = s; Using a pointer is very inconvenient, especially for math structs, I can see things like *result = (*a + *b) * (*c) Would it be nice to extend "inout" to function return, so to have Vector opIndex with the following signature for structs: inout value_type opIndex(index_type index) This helps to make containers more transparent with native array, and much nicer to work with. And extend it to variables: inout SomeStruct s = vector[i]; s.x = 9; Mar 19 2006
Sorry, vector is DTL Vector
struct SomeStruct
{
int x, y, z;
}
Vector!(SomeStruct) vector = new Vector!(SomeStruct)();
to modify entry i, the whole struct needs to be copied from the Vector, modify
the copy, and store the modified copy back into the Vector:
SomeStruct s = vector[i];
vector[i].x = 9; // does nothing meaningful
s.x = 9;
vector[i] = s; // changes entry i
This is inconsistent with the behaviour of array, in the case of array, such
copy out, copy in is not needed:
SomeStruct[] array = new SomeStruct[10];
array[i].x = 9; // changes entry i
For large struct, it is inefficient to copy the whole struct out and copy it
back into the vector
Therefore I propose "inout SomeStruct" as the return type of index operator of
Vector!(SomeStruct), and allow the user to modify the entry directly using the
returned value to achieve array like syntax.
inout SomeStruct opIndex(index_type index)
inout SomeStruct is analogous to inout parameter of function parameters, which
allows a reference to be retrieved from the Vector.
Hong Wing
In article <depxwcj060qq.le9mfgd7onwz$.dlg 40tude.net>, Derek Parnell says...
Mar 19 2006
|