Templates lots of newbie qs.

Chris Warwick sp at m.me.not
Thu Mar 8 09:22:19 PST 2007


"Jarrett Billingsley" <kb3ctd2 at yahoo.com> wrote in message 
news:espbiv$s46$1 at digitalmars.com...
>
> Yeah, for simple stuff like this array properties are probably better. 
> You're also pretty close with your code there;  you can just write it as a 
> templated function:

So templated functions dont need to be inside a template block? You only 
need a template block when you want to group a bunch of stuff together into 
one template, so the whole lot can be created in one go?


> int indexOf(T)(T[] arr, T item)
> {
>    for(int i = 0; i < arr.length; i++)
>    {
>        if (arr[i] == item) { return i; }
>    }
>    return -1;
> }
>
> int[] foo = [0,1,2,3,4];
> int thisdoeswork = foo.indexOf(3);
>
> It's also using IFTI there to determine T implicitly.

IFTI?


> You might check out the Cashew library 
> (http://www.dsource.org/projects/cashew).  It has a bunch of these kinds 
> of array functions in cashew.util.array.

Looks good, I should be able to learn a lot from that.

It's surprised me how very often, even with more advanced features, the D 
syntax seems to be exactly what you would expect it to be. A very good sign 
imo, it seems so intuative straight of the bat.

thanks,

cw 




More information about the Digitalmars-d-learn mailing list