Re: Assignment to this.

David Bernard <[email protected]> Thu, 07 Jul 2005 20:22:14 +0200
Newsgroups gmane.comp.lang.nice.general
Message-ID <[email protected]>
Tom Kaitchuck wrote:

> One thing I have always wished Java would support is assignment to the 
> "this" variable. It is understandable that this could be hard to 
> implement. However it could be useful:
>
> class foo{}
> new foo(Bar b)
> {
>    if( isEvil( b ) )
>    {
>       this = null;
>       return;
>    }
> }
>
> In this case the ability to explicitly return an object from a 
> constructor would serve much the same function, however it would have 
> the disadvantage that it could not be used in an arbitrary method. 
> Though might cause less confusion for multithreaded apps.
> Thoughts?


Why not throw Exception ?

class Foo{}
new Foo(Bar b)
{
   if( isEvil( b ) ) {
      throw new IllegalArgumentException("b is evil");
   }
}

disavantages of this solution is that the constructor could not be call 
in class definition, except if initial assignement is move by the 
compiler into implicit Constructor  :

class Toto {
    Foo foo = new Foo(new Bar());
}


-- 
--------------------------------------------------------------
David "Dwayne" Bernard             Freelance Developer (Java)
                                   mailto:[email protected]
      \|/                          http://dwayne.java-fan.com
--o0O @.@ O0o-------------------------------------------------



-------------------------------------------------------
SF.Net email is sponsored by: Discover Easy Linux Migration Strategies
from IBM. Find simple to follow Roadmaps, straightforward articles,
informative Webcasts and more! Get everything you need to get up to
speed, fast. http://ads.osdn.com/?ad_id=7477&alloc_id=16492&op=click