Re: Re: variable scope question

Daniel Bonniot <[email protected]> Thu, 07 Jul 2005 17:42:02 +0200
Newsgroups gmane.comp.lang.nice.general
Message-ID <[email protected]>
> My suggestion is to delegete to the compiler to declare 'out' in the 
> parent scope of try and set the variable to null.

Hum, yes, it would be possible. But would you do it only for a variable 
declared as the first statement of the try? All of them?

It would also mean that a declaration of a variable with the same name (in the 
finally and after the block) would fail with a "duplicate declaration" error, 
which would be surprising given the  usual rules for scoping...

There is also the question of typing. The type would need to be changed to 
?OutputStream, even if it is declared as OutputStream. Then this would allow 
assigning to null in the try block, which might not be what is wanted.

So I'm not sure what to think. I understand the motivation, I've been annoyed 
with having to declare variables outside the try block, but it seems to need a 
lot of exceptions to handle this properly. Any other opinions?

Actually, often the best would be to use the 'using' method (or to create one 
yourself if you have particular needs, which is possible without tricks, in a 
normal program/library).

using (let out = new ...) {
   ...
}

close is called automatically.

> About the "if (out != null)" if only to avoid NPE in java, but the 
> method close(OutputStream v) (in nice) could be change to 
> close(?OutputStream v) .... => new question is possible in nice to 
> overwrite a method foo(Bar v) with foo(?Bar v) ?

You can achive that, by declaring a new method with the more general type, and 
call the original one in the non-null case:

void close(?OutputStream s);
close(null) {} // do nothing
close(OutputStream s) = s.close(); // s is non-null, this calls the original

> May be a wiki version of the UserManual could be used.

Yes, the problem would be to keep it up to date with the main version. What 
can be done is to add in the wiki a section documenting a new aspect, and to 
transfer it to the user manual later. Of course anybody can also write in a 
copy of the manual.xml docbook file directly, and submit their changes. ;-)

Daniel


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