Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
July 17, 2007 assignment to this | ||||
---|---|---|---|---|
| ||||
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? |
July 17, 2007 Re: assignment to this | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ender KaShae | "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. |
July 17, 2007 Re: assignment to this | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | 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?
>>
> Even if assigning to this did work, it wouldn't solve that problem.
> '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
|
July 18, 2007 Re: assignment to this | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | 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
|
July 18, 2007 Re: assignment to this | ||||
---|---|---|---|---|
| ||||
Posted in reply to Regan Heath | "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. |
July 25, 2007 Re: assignment to this | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | 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.
>
>
*(cast(void**)&this) = cast(void*) __instance__;
works.
Theres no reason to make an artificial restraint for it. Its usefull when patching a COM object's vtable out from under someone.
|
Copyright © 1999-2021 by the D Language Foundation