Transience of .front in input vs. forward ranges

H. S. Teoh hsteoh at quickfur.ath.cx
Sun Nov 4 20:36:32 PST 2012


On Sun, Nov 04, 2012 at 07:47:49PM -0800, Jonathan M Davis wrote:
[...]
> But in either case, all of the range types have to take it into
> account, complicating their implementations that much further, and
> fastRange would complicate those implementations far more than
> isTransient would, because it would require code duplication rather
> than just statically disallowing the transient range when it wouldn't
> work.
[...]

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);
		}
	}


T

-- 
Tech-savvy: euphemism for nerdy.


More information about the Digitalmars-d mailing list