Can't modify this

H. S. Teoh via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Jun 28 14:21:13 PDT 2014


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.


More information about the Digitalmars-d-learn mailing list