March 08, 2007
"Chris Warwick" <sp@m.me.not> wrote in message news:espqa6$1r76$1@digitalmars.com...
>
> "Jarrett Billingsley" <kb3ctd2@yahoo.com> wrote in message news:espjmc$1d51$1@digitalmars.com...
>> "Chris Warwick" <sp@m.me.not> wrote in message news:espgo1$16ge$1@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)
>    {
>        fitems.indexOf(b);   // <--- name clash

should be:

return fitems.indexOf(b);


March 08, 2007
Chris Warwick wrote:
> "Jarrett Billingsley" <kb3ctd2@yahoo.com> wrote in message news:espjmc$1d51$1@digitalmars.com...
> 
>>"Chris Warwick" <sp@m.me.not> wrote in message news:espgo1$16ge$1@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
March 08, 2007
"Kirk McDonald" <kirklin.mcdonald@gmail.com> wrote in message news:esq125$27so$1@digitalmars.com...
> Chris Warwick wrote:
>> "Jarrett Billingsley" <kb3ctd2@yahoo.com> wrote in message news:espjmc$1d51$1@digitalmars.com...
>>
>>>"Chris Warwick" <sp@m.me.not> wrote in message news:espgo1$16ge$1@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


March 08, 2007
"Chris Warwick" <sp@m.me.not> wrote in message news:esq357$2b0j$1@digitalmars.com...
>
> But works fine if I rename the template. Iirc from the docs local scope overrides mixins, so perhaps thats what is happening,

That's exactly what's happening.  Name lookup goes from the inside out, so the compiler finds the class method first, which only takes one parameter, not two.

> 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.

Well keep in mind that

fwidgets.indexOf(foo);

is just syntactic sugar for:

indexOf(fwidgets, foo);

When you write a function that takes an array as the first parameter, it does not mean you are extending the array types.  It's entirely sugar, and function lookup still happens as normal -- from the local scope up.  So all you have to do is rewrite the function call from the property way into the function call way, and use the dot operator to access the globally-scoped symbol.


March 09, 2007
"Jarrett Billingsley" <kb3ctd2@yahoo.com> wrote in message news:esq6gr$2g9p$1@digitalmars.com...
> "Chris Warwick" <sp@m.me.not> wrote in message news:esq357$2b0j$1@digitalmars.com...
>
> is just syntactic sugar for:
>
> indexOf(fwidgets, foo);

Doh! Forgot i could do that lol.

thanks

cw


1 2
Next ›   Last »