try-with-resources and null resource

Serge Boulay serge.boulay at gmail.com
Wed Jan 26 08:43:18 PST 2011


true, but that same example works in c# as Jesper has pointed out. Not that
we
are trying to copy the exact semantics of c# but I do wonder what their
rational was.
Perhaps we can learn from some of the decisions they made (good & bad).

using (IDisposable x = null)
{
    Console.WriteLine("we got here");
}


This example writes "we got here".

On Wed, Jan 26, 2011 at 11:04 AM, Paul Benedict <pbenedict at apache.org>wrote:

> Jesper,
>
> I don't see how ignoring null helps the programmer. What does such a syntax
> mean? As I see it, try-with-resources is about obtaining a resource, doing
> work, and closing it. And since you can't obtain a resource out of a null
> reference, I think that is an error. That's why I prefer NPE.
>
> Paul
>
> 2011/1/26 Jesper Öqvist <jesper.oqvist at cs.lth.se>
>
> > Rémi Forax wrote:
> >
> >> On 01/21/2011 07:24 PM, Paul Benedict wrote:
> >>
> >>
> >>> The NPE is more useful than the language silently doing nothing with a
> >>> null pointer. While your idea is convenient, I believe it assumes too
> much.
> >>> If anything, perhaps the language shouldn't allow an explicit null
> >>> assignment in a try-with-resources block.
> >>>
> >>>
> >>
> >> Hi Paul,
> >> the null assignement is just an example. Perhaps, you prefer this one:
> >>
> >> 4. try(AutoCloseable c = getAnAutoCloseableThatMayBeNull()) {
> >> 5.     // nothing
> >> 6. }
> >>
> >> Rémi
> >>
> >>
> >
> > I am concerned about the consistency between what the programmer expects
> > and what the compiler produces.
> >
> > It seems to me that the following code should be legal, and execute
> without
> > throwing an NPE:
> >
> > try (Resource r = null) {
> >   r = new SomeResource();
> > }
> >
> > However with a null check in the try part of the try-with-resource
> > statement, or an NPE, execution might not pass through the statement as
> the
> > programmer expects it to.
> >
> > Jesper
> >
>
>



More information about the coin-dev mailing list