output ranges: by ref or by value?

Michel Fortin michel.fortin at michelf.com
Thu Dec 31 07:47:35 PST 2009


On 2009-12-31 09:58:06 -0500, Andrei Alexandrescu 
<SeeWebsiteForEmail at erdani.org> said:

> The question of this post is the following: should output ranges be 
> passed by value or by reference? ArrayAppender uses an extra 
> indirection to work properly when passed by value. But if we want to 
> model built-in arrays' operator ~=, we'd need to request that all 
> output ranges be passed by reference.

I think modeling built-in arrays is the way to go as it makes less 
things to learn. In fact, it makes it easier to learn ranges because 
you can begin by learning arrays, then transpose this knowledge to 
ranges which are more abstract and harder to grasp.

Beside, an extra indirection is wasteful when you don't need it. It's 
easier to add a new layer of indirection when you need one than the 
reverse, so the primitive shouldn't require any indirection.


> // pseudo-method
> void put(R, E)(ref R tgt, E e) {
>     tgt.front = e;
>     tgt.popFront();
> }

I like that because it works especially well with arrays. Here's what 
I'm thinking of:

	char[10] buffer;
	char[] remainingSpace = buffer[];
	while (!remainingSpace.empty)
		remainingSpace.put(getc());

	// now buffer is full
	writeln(buffer);

-- 
Michel Fortin
michel.fortin at michelf.com
http://michelf.com/




More information about the Digitalmars-d mailing list