Proposal: takeFront and takeBack

Jonathan M Davis jmdavisProg at gmx.com
Wed Jul 4 13:41:44 PDT 2012


On Wednesday, July 04, 2012 22:34:46 Tobias Pankrath wrote:
> > Yes. But the cost of copying the value and the cost of the
> > exception are
> > separate.
> 
> The argument is not about performance, it's about loosing values.
> 
> > Simply popping off the element could throw an exception (in the
> > case
> > of strings in D, you'll get a UTFException if the string is
> > malformed). So,
> > _regardless_ of whether you return an element, you potentially
> > risk an
> > exception being thrown (depending on the implementation of the
> > container or
> > range).
> 
> It's not the same.
> 
> > And so I think that exceptions are pretty much irrelevant to the
> > discussion of whether returning an element from popFront is a
> > good idea or
> > not.
> 
> How would you implement an returning popFront for a generic
> container that
> does not leak values? It's not possible (at least in C++, not
> sure in D), so it is relevant. Maybe it was not the prime reason,
> but it is a good reason non the less.

Okay. I see what you mean now. I thought that the idea was that you would 
somehow avoid exceptions when popping such that the fact returning the element 
could generate an exception made it then possible to have exceptions be thrown 
from popFront, which would be a problem.

Well, in most cases, I honestly don't think that that would be a problem, 
since if an exception is thrown from the copy constructor / postblit of the 
object being returned, it's probably foobared anyway, and having it on the 
front of the range (or container) would really buy you anything. And if you 
didn't actually want the element, then it doesn't really matter anyway - 
beyond the fact that you now have an extra way to have an exception be thrown. 
But it is true that you avoid that whole issue if you separate returning the 
element from popping it. Regardless, performance alone makes it worthwhile to 
not have popFront return anything IMHO, and that's the argument that I've 
always heard.

- Jonathan M Davis


More information about the Digitalmars-d mailing list