Re: Can't modify this

2014-07-02 Thread Maxim Fomin via Digitalmars-d-learn

On Saturday, 28 June 2014 at 20:40:21 UTC, Ary Borenszweig wrote:

This doesn't work:

class Foo {
  this() {
this = new Foo;
  }
}

Error: Cannot modify 'this'

However you can do this:

class Foo {
  this() {
auto p = &this;
*p = new Foo();
  }
}

It even changes the value of this!

Should that compile? I mean, it's the same as modifying 
'this'...


D language was not aimed toward preventing any attepmt to 
circumvent some hypothetical limitations (it does not even cope 
with things which it should definetely prevent). There are holes 
much - much worse. And changing value of this parameter is not a 
problem anyway, since it is possible to have typeid(this) != 
typeid of type of current method which is bigger problem than 
pointing to different value of same type.


Re: Can't modify this

2014-06-30 Thread via Digitalmars-d-learn

On Saturday, 28 June 2014 at 21:39:35 UTC, Ary Borenszweig wrote:

On 6/28/14, 6:21 PM, H. S. Teoh via Digitalmars-d-learn wrote:
On Sat, Jun 28, 2014 at 05:40:19PM -0300, Ary Borenszweig via 
Digitalmars-d-learn wrote:

This doesn't work:

class Foo {
  this() {
this = new Foo;
  }
}

Error: Cannot modify 'this'

However you can do this:

class Foo {
  this() {
auto p = &this;
*p = new Foo();
  }
}

It even changes the value of this!

Should that compile? I mean, it's the same as modifying 
'this'...


I'd say, file an enhancement request on the bug tracker.

However, there comes a point, where given enough indirections, 
it would
be infeasible for the compiler to figure out exactly where 
everything
points, and so you'll be able to circumvent the compiler check 
somehow.
If you're out to thwart the compiler, then eventually you will 
succeed,

but it begs the question, why?


T


I think that if you disallow taking the address of `this`, then 
the problem is solved.


IMO it shouldn't be treated differently from other parameters - 
that's what `this` is, after all.


Re: Can't modify this

2014-06-30 Thread H. S. Teoh via Digitalmars-d-learn
On Sat, Jun 28, 2014 at 06:39:33PM -0300, Ary Borenszweig via 
Digitalmars-d-learn wrote:
> On 6/28/14, 6:21 PM, H. S. Teoh via Digitalmars-d-learn wrote:
> >On Sat, Jun 28, 2014 at 05:40:19PM -0300, Ary Borenszweig via 
> >Digitalmars-d-learn wrote:
> >>This doesn't work:
> >>
> >>class Foo {
> >>   this() {
> >> this = new Foo;
> >>   }
> >>}
> >>
> >>Error: Cannot modify 'this'
> >>
> >>However you can do this:
> >>
> >>class Foo {
> >>   this() {
> >> auto p = &this;
> >> *p = new Foo();
> >>   }
> >>}
> >>
> >>It even changes the value of this!
> >>
> >>Should that compile? I mean, it's the same as modifying 'this'...
> >
> >I'd say, file an enhancement request on the bug tracker.
> >
> >However, there comes a point, where given enough indirections, it
> >would be infeasible for the compiler to figure out exactly where
> >everything points, and so you'll be able to circumvent the compiler
> >check somehow.  If you're out to thwart the compiler, then eventually
> >you will succeed, but it begs the question, why?
> >
> >
> >T
> 
> I think that if you disallow taking the address of `this`, then the
> problem is solved.
> 
> This is not a big issue (more a curiosity). I just wanted to know what
> is the correct way to do in this case.

That depends on what you're trying to do. Why do you need to assign to
this?


T

-- 
Three out of two people have difficulties with fractions. -- Dirk Eddelbuettel


Re: Can't modify this

2014-06-28 Thread Ary Borenszweig via Digitalmars-d-learn

On 6/28/14, 6:21 PM, H. S. Teoh via Digitalmars-d-learn wrote:

On Sat, Jun 28, 2014 at 05:40:19PM -0300, Ary Borenszweig via 
Digitalmars-d-learn wrote:

This doesn't work:

class Foo {
   this() {
 this = new Foo;
   }
}

Error: Cannot modify 'this'

However you can do this:

class Foo {
   this() {
 auto p = &this;
 *p = new Foo();
   }
}

It even changes the value of this!

Should that compile? I mean, it's the same as modifying 'this'...


I'd say, file an enhancement request on the bug tracker.

However, there comes a point, where given enough indirections, it would
be infeasible for the compiler to figure out exactly where everything
points, and so you'll be able to circumvent the compiler check somehow.
If you're out to thwart the compiler, then eventually you will succeed,
but it begs the question, why?


T


I think that if you disallow taking the address of `this`, then the 
problem is solved.


This is not a big issue (more a curiosity). I just wanted to know what 
is the correct way to do in this case.




Re: Can't modify this

2014-06-28 Thread H. S. Teoh via Digitalmars-d-learn
On Sat, Jun 28, 2014 at 05:40:19PM -0300, Ary Borenszweig via 
Digitalmars-d-learn wrote:
> This doesn't work:
> 
> class Foo {
>   this() {
> this = new Foo;
>   }
> }
> 
> Error: Cannot modify 'this'
> 
> However you can do this:
> 
> class Foo {
>   this() {
> auto p = &this;
> *p = new Foo();
>   }
> }
> 
> It even changes the value of this!
> 
> Should that compile? I mean, it's the same as modifying 'this'...

I'd say, file an enhancement request on the bug tracker.

However, there comes a point, where given enough indirections, it would
be infeasible for the compiler to figure out exactly where everything
points, and so you'll be able to circumvent the compiler check somehow.
If you're out to thwart the compiler, then eventually you will succeed,
but it begs the question, why?


T

-- 
Never wrestle a pig. You both get covered in mud, and the pig likes it.


Can't modify this

2014-06-28 Thread Ary Borenszweig via Digitalmars-d-learn

This doesn't work:

class Foo {
  this() {
this = new Foo;
  }
}

Error: Cannot modify 'this'

However you can do this:

class Foo {
  this() {
auto p = &this;
*p = new Foo();
  }
}

It even changes the value of this!

Should that compile? I mean, it's the same as modifying 'this'...