www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - assignment to this

reply Ender KaShae <astrothayne gmail.com> writes:
to my utter astonishment when I compiled a program with an assignment to this
did ABSOLUTELY NOTHING.  It seems to me that this is bug, the statement should
either execute or give an error when there is an assignment to this.  

since my idea of assigning to this fell through I am wondering if there is
another way that I can give an immutable type opPostInc, opAddAssign, etc.
without altering all references to it.  any ideas?
Jul 17 2007
parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Ender KaShae" <astrothayne gmail.com> wrote in message 
news:f7j521$1dnc$1 digitalmars.com...
 since my idea of assigning to this fell through I am wondering if there is 
 another way that I can give an immutable type opPostInc, opAddAssign, etc. 
 without altering all references to it.  any ideas?

Even if assigning to this did work, it wouldn't solve that problem. 'this' is just another local parameter to a member function.
Jul 17 2007
next sibling parent BCS <ao pathlink.com> writes:
Reply to Jarrett,

 "Ender KaShae" <astrothayne gmail.com> wrote in message
 news:f7j521$1dnc$1 digitalmars.com...
 
 since my idea of assigning to this fell through I am wondering if
 there is another way that I can give an immutable type opPostInc,
 opAddAssign, etc. without altering all references to it.  any ideas?
 

'this' is just another local parameter to a member function.

that is in fact exactly what happens: |import std.stdio; |class Foo |{ | int i; | void go() | { | writef("%d\n", i); | this = foo; | writef("%d\n", i); | } |} |Foo foo; |void main() |{ | Foo f = new Foo; f.i=1; | foo = new Foo; foo.i=2; | f.go(); | f.go(); |} output: 1 2 1 2
Jul 17 2007
prev sibling parent reply Regan Heath <regan netmail.co.nz> writes:
Jarrett Billingsley wrote:
 "Ender KaShae" <astrothayne gmail.com> wrote in message 
 news:f7j521$1dnc$1 digitalmars.com...
 since my idea of assigning to this fell through I am wondering if there is 
 another way that I can give an immutable type opPostInc, opAddAssign, etc. 
 without altering all references to it.  any ideas?

Even if assigning to this did work, it wouldn't solve that problem. 'this' is just another local parameter to a member function.

Which is probably exactly why it doesn't work. I agree with the OP tho, it should either work as you'd expect or error. In other words: - 'this' could be made 'ref' - 'this' could be made 'final' Regan
Jul 18 2007
parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Regan Heath" <regan netmail.co.nz> wrote in message 
news:f7khch$tqs$1 digitalmars.com...
 I agree with the OP tho, it should either work as you'd expect or error.

Assigning to 'this' if the aggregate has an overloaded opAssign is perfectly fine; but yes, I think that trying to reassign which instance 'this' points to should be an error.
Jul 18 2007
parent Neal Alexander <wqeqweuqy hotmail.com> writes:
Jarrett Billingsley wrote:
 "Regan Heath" <regan netmail.co.nz> wrote in message 
 news:f7khch$tqs$1 digitalmars.com...
 I agree with the OP tho, it should either work as you'd expect or error.

Assigning to 'this' if the aggregate has an overloaded opAssign is perfectly fine; but yes, I think that trying to reassign which instance 'this' points to should be an error.

works. Theres no reason to make an artificial restraint for it. Its usefull when patching a COM object's vtable out from under someone.
Jul 24 2007