Re: Assignment to this.

Tom Kaitchuck <[email protected]> Thu, 07 Jul 2005 11:45:29 -0700
Newsgroups gmane.comp.lang.nice.general
Message-ID <[email protected]>
David Bernard wrote:

> 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());
> }
>
>
I would like to reduce my feature request to simply retuning values 
explicitly from a constructor, as much of the functionality of "this" 
assignment can be done with multimethods:

combine (fooBar a, fooBar b)
{
    fooBar c = new fooBar();
    c.foo = a.foo;
    c.bar = b.bar;
    a = b = c;
}


-------------------------------------------------------
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