Templates lots of newbie qs.

Chris Warwick sp at m.me.not
Thu Mar 8 14:36:33 PST 2007


"Kirk McDonald" <kirklin.mcdonald at gmail.com> wrote in message 
news:esq125$27so$1 at digitalmars.com...
> Chris Warwick wrote:
>> "Jarrett Billingsley" <kb3ctd2 at yahoo.com> wrote in message 
>> news:espjmc$1d51$1 at digitalmars.com...
>>
>>>"Chris Warwick" <sp at m.me.not> wrote in message 
>>>news:espgo1$16ge$1 at digitalmars.com...
>>>
>>>>IFTI?
>>>
>>>...you can just type "functionName(params)".
>>>
>>>IFTI stands for "Implicit Function Template Instantiation."  When you 
>>>write a templated function:
>>>
>>>T func(T)(T val)
>>>{
>>>   return val;
>>>}
>>>
>>>You don't want to have to type out:
>>>
>>>func!(int)(4);
>>>func!(char[])("hi");
>>>
>>>So IFTI can implicitly determine what T should be by looking at the 
>>>parameter list:
>>>
>>>func(4); // T is determined to be int
>>>func("hello"); // T is determined to be char[]
>>>
>>>It's a very useful feature.
>>
>>
>> So say you have the following...
>>
>> int indexOf(T)(T[] arr, T item)
>> {
>>     // search for and return index
>> }
>>
>> class Foo()
>> {
>>     Bar[] fitems;
>>     int indexOf(Bar b)
>>     {
>>         return fitems.indexOf(b);   // <--- name clash
>>     }
>> }
>>
>> How do you get fitems.indexOf to resolve to the templated array property 
>> and not the local function of the same name? Obviously it should resolve 
>> to the local symbol but i cant figure out how to help teh compiler know i 
>> want it to use the template version of indexOf.
>>
>> thanks,
>>
>> cw
>
> I would expect that to resolve to the global function template, since it 
> is looking for a function whose parameters are (Bar[], Bar). The nested 
> function is not a match for this.

int indexOf(T)(T[] arr, T item)
{
    for(int i = 0; i < arr.length; i++)
    {
        if (arr[i] == item) { return i; }
    }
    return -1;
}

class TComponent
{

    TWidget[] fwidgets;

    int indexOf(TWidget widget)
    {
        assert(widget != null);
        return fwidgets.indexOf(widget);   // line 118
    }
}

C:\Documents and Settings\chris\Desktop\dproj>bud Project1.d
cjl\component.d(118): function cjl.component.TComponent.indexOf (TWidget) 
does n
ot match parameter types (TWidget[],TWidget)
cjl\component.d(118): Error: expected 1 arguments, not 2
cjl\component.d(118): Error: cannot implicitly convert expression 
(this.fwidgets
) of type TWidget[] to cjl.component.TWidget

C:\Documents and Settings\chris\Desktop\dproj>pause
Press any key to continue . . .

But works fine if I rename the template. Iirc from the docs local scope 
overrides mixins, so perhaps thats what is happening,


> If the nested function /was/ a match, I would expect it to call it 
> recursively. To explicitly call the global function, you would have to 
> call it like a regular function and use the unary dot operator:
>
> .indexOf(fitems, b);
>
> That dot tells it to look at the global scope.

Yeah but the problem is i am using it as an Array Property so it's already 
got a dot to seperate it from the array.

cheers,

cw




More information about the Digitalmars-d-learn mailing list