www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - ChainImpl save method

reply Andrej Mitrovic <none none.none> writes:
I'm having trouble understanding this code in std.range.chainImpl:

         property ChainImpl save()
        {
            auto result = ChainImpl();
            foreach (i, Unused; R)
            {
                result._input[i] = _input[i].save;
            }
            return result;
        }

Specifically, line 1559 which is:
auto result = ChainImpl();

Let me see if I got my hunch right:

Is this simply instantiating another implementation of ChainImpl with the same
arguments? e.g. inside an already instantiated template with ChainImpl(Range1,
Range2), would ChainImpl() simply instantiate another ChainImpl template with
the same compile-time arguments? Of course, it wouldn't copy any runtime values
since this is all done at compile-time and they don't exist yet.

Btw, I'm having a kick-ass time understanding Phobos as of lately. It only took
me a couple of months but I am slowly grokking templates, even though I've
*never* used templates before I tried D. 
Dec 29 2010
parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Wed, 29 Dec 2010 15:26:25 -0500, Andrej Mitrovic <none none.none> wrote:

 I'm having trouble understanding this code in std.range.chainImpl:

          property ChainImpl save()
         {
             auto result = ChainImpl();
             foreach (i, Unused; R)
             {
                 result._input[i] = _input[i].save;
             }
             return result;
         }

 Specifically, line 1559 which is:
 auto result = ChainImpl();

 Let me see if I got my hunch right:

 Is this simply instantiating another implementation of ChainImpl with  
 the same arguments? e.g. inside an already instantiated template with  
 ChainImpl(Range1, Range2), would ChainImpl() simply instantiate another  
 ChainImpl template with the same compile-time arguments? Of course, it  
 wouldn't copy any runtime values since this is all done at compile-time  
 and they don't exist yet.
Yes. Inside a template, the name of the template is synonymous with the template instance being instantiated. So for example: struct S(T) { void foo(){ S s; // this is of type S!T } } -Steve
Dec 29 2010
next sibling parent spir <denis.spir gmail.com> writes:
On Wed, 29 Dec 2010 15:35:31 -0500
"Steven Schveighoffer" <schveiguy yahoo.com> wrote:

 Yes.  Inside a template, the name of the template is synonymous with the =
=20
 template instance being instantiated.
=20
 So for example:
=20
 struct S(T)
 {
     void foo(){ S s; // this is of type S!T
     }
 }
Good to know ;-) I write specialised types everywhere in template, like struct S(T) { S!T aNewSBangT (S!T anotherSBangT) {...} } Denis -- -- -- -- -- -- -- vit esse estrany =E2=98=A3 spir.wikidot.com
Dec 29 2010
prev sibling parent reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
One more question. What does this do?

static RvalueElementType fixRef(RvalueElementType val)
{
    return val;
}

This looks like a workaround for something but I can't figure out why
the function is static and why does it just return the value passed
in?

On 12/29/10, spir <denis.spir gmail.com> wrote:
 On Wed, 29 Dec 2010 15:35:31 -0500
 "Steven Schveighoffer" <schveiguy yahoo.com> wrote:

 Yes.  Inside a template, the name of the template is synonymous with the
 template instance being instantiated.

 So for example:

 struct S(T)
 {
     void foo(){ S s; // this is of type S!T
     }
 }
Good to know ;-) I write specialised types everywhere in template, like struct S(T) { S!T aNewSBangT (S!T anotherSBangT) {...} } Denis -- -- -- -- -- -- -- vit esse estrany =E2=98=A3 spir.wikidot.com
Dec 29 2010
parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Wed, 29 Dec 2010 17:35:05 -0500, Andrej Mitrovic  
<andrej.mitrovich gmail.com> wrote:

 One more question. What does this do?

 static RvalueElementType fixRef(RvalueElementType val)
 {
     return val;
 }

 This looks like a workaround for something but I can't figure out why
 the function is static and why does it just return the value passed
 in?
A static function inside a struct or class does not have a 'this' reference. It's like a free function (one outside a class or struct definition) but is within the namespace of the struct or class. Why it is there? I think because a chain range's components may all return by ref, or at least one may return by value. If at least one returns by value, it logically follows that the chain range itself must return by value. If all return by ref, it's ok for the chain range to return by ref. So why does the fixRef exist? Probably because the compile-time checks used to determine whether you can return by ref or return by value are verbose, and fixRef is used in many places. I'm only speculating, I didn't write it. With proper compiler inlining, the fixRef function gets optimized out, but I think ref functions do not get inlined at the moment. -Steve
Dec 30 2010