Templates lots of newbie qs.

Kirk McDonald kirklin.mcdonald at gmail.com
Thu Mar 8 14:03:34 PST 2007


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.

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.

-- 
Kirk McDonald
http://kirkmcdonald.blogspot.com
Pyd: Connecting D and Python
http://pyd.dsource.org


More information about the Digitalmars-d-learn mailing list