Eigenpoll : D wish list : inout variable and return 

Extend inout to variables and function return.

Allow syntax such as
struct Foo
{
int x, int y, int z;
}
inout Foo foo()
{
return m_foo;
}

such that the returned value can be used to modify the original struct.

Rationale, for the example below,

class Bar
{
private:
Foo m_foo;
public:
Foo foo() { return m_foo; }
Foo foo(Foo value) { return m_foo = value; }
}

now if i want to change the value of m_foo.x to 5, i have to do

Foo foo = bar.foo;
foo.x = 5;
bar.foo = foo;

it would be nice if I can define a return type as "inout Foo"
inout Foo foo() { return m_foo;}

To extend it further, one might want to define inout variables as
inout Foo foo = bar.foo;

Advantages:

1. efficiency, one can directly change the states of the struct instead of making a copy first, change the copy, and put the result back to the struct

2. less buggy, having to type 3 lines always increases chance of bugs, also, someone might inadvertantly do bar.foo.x = 5 and forgets that he is working on a copy

3. reduce the use of pointers, the above can be achieved using pointers, but it means defining two versions of the same method every time a struct property is written.


Report this item for cleanup