Thread overview
D struct are difficult to work with!
Mar 19, 2006
Hong Wing
Mar 19, 2006
Hasan Aljudy
Mar 20, 2006
Derek Parnell
Mar 20, 2006
Hong Wing
March 19, 2006
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;



March 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;
> 
> 
> 

is there a particular problem with using a class rather than a struct?
March 20, 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;

I'm sorry but I don't know what you are meaning by 'vector' in this context? Do you mean a reference to an array of structs? Can you show me some real code that demonstrates the issue for you.

-- 
Derek
(skype: derek.j.parnell)
Melbourne, Australia
"Down with mediocracy!"
20/03/2006 11:08:55 AM
March 20, 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...
>
>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;
>
>I'm sorry but I don't know what you are meaning by 'vector' in this context? Do you mean a reference to an array of structs? Can you show me some real code that demonstrates the issue for you.
>
>-- 
>Derek
>(skype: derek.j.parnell)
>Melbourne, Australia
>"Down with mediocracy!"
>20/03/2006 11:08:55 AM