www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - lvalue forwarding struct?

reply Chad J <gamerChad _spamIsBad_gmail.com> writes:
What I am looking for is a templated struct that simply wraps a 
variable, and ensures lvalueness is preserved for that variable.
Ex:

foo(bar) *= 9;

In this case foo is to be a function that acts on bar, and then makes 
sure that bar has the *= 9; applied to it.

It might look like this:

int foo(inout int input)
{
   input++;
   return lvalue(input);
/* lvalue(...) is an opCall constructor for the lvalue templated 
forwarding struct.  */
}

Thus we could write

int bar = 3;
foo(bar) *= 9;
printf( "%d", bar ); // prints 36

One possible use case would be as a hack to get around certain 
shortcomings of properties for the time being.

So does anyone have such a thing that they want to share?
Otherwise this is something I might do.
Aug 21 2007
next sibling parent reply Bill Baxter <dnewsgroup billbaxter.com> writes:
Chad J wrote:
 What I am looking for is a templated struct that simply wraps a 
 variable, and ensures lvalueness is preserved for that variable.
 Ex:
 
 foo(bar) *= 9;
 
 In this case foo is to be a function that acts on bar, and then makes 
 sure that bar has the *= 9; applied to it.
 
 It might look like this:
 
 int foo(inout int input)
 {
   input++;
   return lvalue(input);
 /* lvalue(...) is an opCall constructor for the lvalue templated 
 forwarding struct.  */
 }
It certainly isn't going to work if you return 'int'.
 Thus we could write
 
 int bar = 3;
 foo(bar) *= 9;
 printf( "%d", bar ); // prints 36
 
 One possible use case would be as a hack to get around certain 
 shortcomings of properties for the time being.
 
 So does anyone have such a thing that they want to share?
 Otherwise this is something I might do.
I think you should take a look at the code posted by Reiner the other day in digitalmars.D under the subject "Proxy objects and controlling/monitoring access". The thread starts with the message Xref: digitalmars.com digitalmars.D:57028 --bb
Aug 21 2007
parent Chad J <gamerChad _spamIsBad_gmail.com> writes:
Bill Baxter wrote:
 Chad J wrote:
 What I am looking for is a templated struct that simply wraps a 
 variable, and ensures lvalueness is preserved for that variable.
 Ex:

 foo(bar) *= 9;

 In this case foo is to be a function that acts on bar, and then makes 
 sure that bar has the *= 9; applied to it.

 It might look like this:

 int foo(inout int input)
 {
   input++;
   return lvalue(input);
 /* lvalue(...) is an opCall constructor for the lvalue templated 
 forwarding struct.  */
 }
It certainly isn't going to work if you return 'int'.
 Thus we could write

 int bar = 3;
 foo(bar) *= 9;
 printf( "%d", bar ); // prints 36

 One possible use case would be as a hack to get around certain 
 shortcomings of properties for the time being.

 So does anyone have such a thing that they want to share?
 Otherwise this is something I might do.
I think you should take a look at the code posted by Reiner the other day in digitalmars.D under the subject "Proxy objects and controlling/monitoring access". The thread starts with the message Xref: digitalmars.com digitalmars.D:57028 --bb
Looks interesting. Thanks.
Aug 21 2007
prev sibling parent reply Henning Hasemann <hhasemann web.de> writes:
 So does anyone have such a thing that they want to share?
http://www.digitalmars.com/d/archives/digitalmars/D/Problem_with_Point_property_49990.html#N50017 -- GPG Public Key: http://keyserver.ganneff.de:11371/pks/lookup?op=get&search=0xDDD6D36D41911851 Fingerprint: 344F 4072 F038 BB9E B35D E6AB DDD6 D36D 4191 1851
Aug 21 2007
parent Chad J <gamerChad _spamIsBad_gmail.com> writes:
Henning Hasemann wrote:
 So does anyone have such a thing that they want to share?
http://www.digitalmars.com/d/archives/digitalmars/D/Problem_with_Point_property_49990.html#N50017
As Chris said, rather slick. Thanks.
Aug 21 2007