Re: Re: variable scope question

David Bernard <[email protected]> Thu, 07 Jul 2005 18:18:35 +0200
Newsgroups gmane.comp.lang.nice.general
Message-ID <[email protected]>
Daniel Bonniot wrote:

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

For all variable used in catch and finally clause

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

only for variable variable after the block, expect if we considere same 
scope for try/catch/finally clause... it was only a sugar syntax suggestion

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

+1

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

like in boo.
What append if I need several closable object ?
using (let out = new ...) {
    using (let in = new ...) {
    }
}

>
>> 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
>
Great like in Erlang, I notice the possibility in the doc but I forgot 
it, after. Lot of feature in Nice.

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

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