On Wed, Mar 30, 2016 at 2:17 PM, Neil Conway <[email protected]> wrote:
> On Wed, Mar 30, 2016 at 4:57 PM, Benjamin Mahler <[email protected]> > wrote: > > Yikes! (3) being not true to me means that I needed non-local reasoning > to > > determine the optionality. > > Sorry: to clarify, I didn't mean "there is not always a latch" in the > code in question. I meant: "writing 'delete latch;' is not a good way > to imply that there is always a latch, because that code works fine if > latch is optional." > > int fd = -1; > > ... > > if (something) { fd = open(); } > > ... > > if (fd != -1) { close(fd); } > > > > Is there something fundamentally different about new/delete? > > To me, `if (ptr != NULL) { delete ptr; }` is distracting, because the > fact that deleting a null pointer is well-defined is very well known. > Well close(-1) is also well-defined, and isn't "very well known" arguable? I did not know this off the top of my head but I could guess it, and Anand apparently learned this recently when I chatted with him. One could say that close(-1) very well known to be a no-op, at least it is to me, but why make the reader have to think about this kind of detail? > So I immediately see that and wonder why there is obviously redundant > code. In contrast, close(-1) not having some subtle overloaded meaning > (like kill(-1) does) isn't as obvious. > > > Generally we use options for optionality, however we've occasionally > > avoided Option<T*> in favor of T* out of convenience. For these > Option<T*> > > variables masquerading as T* variables, it would be great to keep the > > optionality checks to help the reader intuit the optionality, or convert > to > > something better than just naked deletes. > > I definitely think there is value in distinguishing between optional > and non-optional pointer values, but I don't think no-op statements > like `if (ptr) { delete ptr; }` are the best way to do that. +1 on > using something better than just naked deletes! > I think we're agreed on the ideal. For the proposal of changing the existing approach to deletion of pointers, I can see a lot of code using null guards around deletes and I'm not convinced that removing the guards makes life easier for the reader in many cases. If we wanted to remove them, we should propose such a change first and vote on it. Is there a lot of code relying on naked deletes? > > Neil >
