September 04, 2010 Re: this as lvalue? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Andrei Alexandrescu | On Sep 4, 10 05:22, Andrei Alexandrescu wrote:
> On 9/3/10 16:03 CDT, bearophile wrote:
>> JMRyan:
>>> Is this a bug in the compiler (v.2.047)? Am I missing something in
>>> thinking it shouldn't be?
>>
>> I think it's not a bug. It's not a common need, but a method may way
>> want to swap this with another. In Phobos this is done on a struct, see:
>> http://www.dsource.org/projects/phobos/browser/trunk/phobos/std/stdio.d#L324
>>
>>
>> Bye,
>> bearophile
>
> For classes this must be an rvalue.
>
> Andrei
If 'this' is an rvalue then it is not possible to take the address ('&this').
| |||
September 04, 2010 Re: this as lvalue? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to KennyTM~ | On Saturday 04 September 2010 01:32:03 KennyTM~ wrote:
> On Sep 4, 10 05:22, Andrei Alexandrescu wrote:
> > On 9/3/10 16:03 CDT, bearophile wrote:
> >> JMRyan:
> >>> Is this a bug in the compiler (v.2.047)? Am I missing something in
> >>> thinking it shouldn't be?
> >>
> >> I think it's not a bug. It's not a common need, but a method may way want to swap this with another. In Phobos this is done on a struct, see: http://www.dsource.org/projects/phobos/browser/trunk/phobos/std/stdio.d# L324
> >>
> >>
> >> Bye,
> >> bearophile
> >
> > For classes this must be an rvalue.
> >
> > Andrei
>
> If 'this' is an rvalue then it is not possible to take the address
> ('&this').
And of what possible use would taking the address of an object reference be?
- Jonathan M Davis
| |||
September 04, 2010 Re: this as lvalue? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to KennyTM~ | On 9/4/10 3:32 CDT, KennyTM~ wrote:
> On Sep 4, 10 05:22, Andrei Alexandrescu wrote:
>> On 9/3/10 16:03 CDT, bearophile wrote:
>>> JMRyan:
>>>> Is this a bug in the compiler (v.2.047)? Am I missing something in
>>>> thinking it shouldn't be?
>>>
>>> I think it's not a bug. It's not a common need, but a method may way
>>> want to swap this with another. In Phobos this is done on a struct, see:
>>> http://www.dsource.org/projects/phobos/browser/trunk/phobos/std/stdio.d#L324
>>>
>>>
>>>
>>> Bye,
>>> bearophile
>>
>> For classes this must be an rvalue.
>>
>> Andrei
>
> If 'this' is an rvalue then it is not possible to take the address
> ('&this').
And I think you shouldn't do that either!
Andrei
| |||
September 05, 2010 Re: this as lvalue? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Andrei Alexandrescu | Andrei Alexandrescu <SeeWebsiteForEmail@erdani.org> wrote in news:i5rovm$1q4b$1@digitalmars.com: > > For classes this must be an rvalue. > > Andrei I reported this as issue 4819. | |||
September 05, 2010 Re: this as lvalue? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to JMRyan | Does this mean assigning to fields won't be an option anymore when using this?
E.g.:
class Foo
{
int x;
int y;
void changeXY(int x, int y)
{
this.x = x;
this.y = y;
}
}
On Sun, Sep 5, 2010 at 6:09 PM, JMRyan <nospam@nospam.com> wrote:
> Andrei Alexandrescu <SeeWebsiteForEmail@erdani.org> wrote in news:i5rovm$1q4b$1@digitalmars.com:
>>
>> For classes this must be an rvalue.
>>
>> Andrei
>
> I reported this as issue 4819.
>
| |||
September 05, 2010 Re: this as lvalue? | ||||
|---|---|---|---|---|
| ||||
Gmail likes to eat my code for some reason, it just ate two closing parantheses.. lol.
On Mon, Sep 6, 2010 at 12:02 AM, Andrej Mitrovic <andrej.mitrovich@gmail.com> wrote:
> Does this mean assigning to fields won't be an option anymore when using this?
>
> E.g.:
>
> class Foo
> {
> int x;
> int y;
>
> void changeXY(int x, int y)
> {
> this.x = x;
> this.y = y;
> }
> }
>
> On Sun, Sep 5, 2010 at 6:09 PM, JMRyan <nospam@nospam.com> wrote:
>> Andrei Alexandrescu <SeeWebsiteForEmail@erdani.org> wrote in news:i5rovm$1q4b$1@digitalmars.com:
>>>
>>> For classes this must be an rvalue.
>>>
>>> Andrei
>>
>> I reported this as issue 4819.
>>
>
| ||||
September 05, 2010 Re: this as lvalue? | ||||
|---|---|---|---|---|
| ||||
On Sunday 05 September 2010 15:02:10 Andrej Mitrovic wrote:
> Does this mean assigning to fields won't be an option anymore when using this?
>
> E.g.:
>
> class Foo
> {
> int x;
> int y;
>
> void changeXY(int x, int y)
> {
> this.x = x;
> this.y = y;
> }
> }
No, it simply means that you won't be able to do this in a class:
this = new MyObj();
In C++, this is a const pointer to non-const data in non-const functions and a const pointer to const data in const functions. The problem is that due to how D deals with const and references, you can't have this be a const reference to non-const data. And with the current implementation, that means that you can reassign this. Now, that's setting a local variable, so it only affects that function and any other member functions that it calls, but it's still not a good thing. If this in classes becomes an rvalue semantically-speaking (regardless of how its done under the hood), then you won't be able to assign to it anymore. But you should still be able to assign to anything that you get from it. Remember that this.x is dereferencing the this reference to get at the memory where x is. Whether you can assign to this is irrelevant for that. It's not trying to do anything to this itself, just what it refers to.
- Jonathan M Davis
| ||||
September 05, 2010 Re: this as lvalue? | ||||
|---|---|---|---|---|
| ||||
Ok, thanks. I never liked the "lvalue rvalue" names.. For example http://en.wikipedia.org/wiki/Value_%28computer_science%29 l-value, non-lvalue, rvalue, nonlvalue.. bleh. It's the same thing as when I see "mutable, non-immutable, non-mutable, immutable" sprinkled with a few tripple negatives all around in a piece of text. /non-non-rant On Mon, Sep 6, 2010 at 12:46 AM, Jonathan M Davis <jmdavisprog@gmail.com> wrote: > On Sunday 05 September 2010 15:02:10 Andrej Mitrovic wrote: >> Does this mean assigning to fields won't be an option anymore when using this? >> >> E.g.: >> >> class Foo >> { >> int x; >> int y; >> >> void changeXY(int x, int y) >> { >> this.x = x; >> this.y = y; >> } >> } > > No, it simply means that you won't be able to do this in a class: > > this = new MyObj(); > > > In C++, this is a const pointer to non-const data in non-const functions and a const pointer to const data in const functions. The problem is that due to how D deals with const and references, you can't have this be a const reference to non-const data. And with the current implementation, that means that you can reassign this. Now, that's setting a local variable, so it only affects that function and any other member functions that it calls, but it's still not a good thing. If this in classes becomes an rvalue semantically-speaking (regardless of how its done under the hood), then you won't be able to assign to it anymore. But you should still be able to assign to anything that you get from it. Remember that this.x is dereferencing the this reference to get at the memory where x is. Whether you can assign to this is irrelevant for that. It's not trying to do anything to this itself, just what it refers to. > > - Jonathan M Davis > | ||||
September 06, 2010 Re: this as lvalue? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Andrej Mitrovic | On 09/05/2010 05:02 PM, Andrej Mitrovic wrote:
> Does this mean assigning to fields won't be an option anymore when using this?
>
> E.g.:
>
> class Foo
> {
> int x;
> int y;
>
> void changeXY(int x, int y)
> {
> this.x = x;
> this.y = y;
> }
> }
No, guys, most everything will be the same. If you can write (new Foo).x = x, then you can also write this.x = x. Try it now!
Andrei
| |||
September 06, 2010 Re: this as lvalue? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Andrei Alexandrescu | Andrei: > If you can write (new Foo).x = x, then you can also write this.x = x. Try it now! See my bug reports/enhancement requests (about three days ago in a program of mine I have added a bug that enhancement 4407 is able to avoid): Arguments and attributes with the same name http://d.puremagic.com/issues/show_bug.cgi?id=3878 Catch wrong argument<->attribute assignments in methods http://d.puremagic.com/issues/show_bug.cgi?id=4407 Bye, bearophile | |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply