Transience of .front in input vs. forward ranges

H. S. Teoh hsteoh at quickfur.ath.cx
Sun Nov 4 21:07:46 PST 2012


On Sun, Nov 04, 2012 at 08:49:53PM -0800, Jonathan M Davis wrote:
> On Sunday, November 04, 2012 20:36:32 H. S. Teoh wrote:
> > No code duplication is actually necessary, even when the range is
> > transient-capable. For example:
> > 
> > 	// New version of byLine
> > 	struct ByLine {
> > 		char[] buffer;
> > 
> > 		@property bool empty() {
> > 			...
> > 		}
> > 
> > 		@property char[] front() {
> > 			// NOTE: non-transient by default
> > 			return buffer.dup;
> > 		}
> > 
> > 		void popFront() {
> > 			...
> > 		}
> > 
> > 		auto transient() {
> > 			struct TransientByLine {
> > 				ByLine innerRange;
> > 
> > 				// This is the only code that needs to
> > 				// be "duplicated". Because it's
> > 				// actually different!
> > 				@property char[] front() {
> > 					return innerRange.buffer;
> > 				}
> > 
> > 				// empty, popFront, etc., just "inherit"
> > 				// from the inner range. No code
> > 				// duplication needed.
> > 				alias innerRange this;
> > 			}
> > 			return TransientByLine(this);
> > 		}
> > 	}
> 
> How is that not duplicated? You have to create a second range type to
> support the transience. Sure, it's a thin wrapper, but it's yet more
> code that has to be put in a potentially large number of range types.

Stop right there. You're contradicting yourself. You kept saying that
transient ranges are rare, abnormal, etc., and now you say they are a
potentially large number of range types? I'm sorry, you've managed to
utterly confuse me.  Are they rare, or are they not?

If they are rare, then this thin wrapper only needs to be written in
those few rare cases. *No other range type needs to be changed.*

If they are common, then they aren't abnormal, and we should be fixing
Phobos to deal with them. Everywhere. In a pervasive, invasive, large
changeset, because almost all current code is broken w.r.t. to these
common cases -- if indeed they are common.


> It's yet one more thing that has to been maintained and tested. We
> need to be simplifying ranges, not complicating them yet further.
> They're already seriously pushing it in terms of how complicated they
> are. How many people outside of this newsgroup actually, properly
> understand ranges as it is?
[...]

As I said, by having a UFCS version of .transient that simply returns
the original range, you don't even need to know what .transient does.
Don't even include it in your range, and it just behaves as a normal
non-transient range. How is that any more complicated?


T

-- 
Bare foot: (n.) A device for locating thumb tacks on the floor.


More information about the Digitalmars-d mailing list