Thread overview | |||||||||
---|---|---|---|---|---|---|---|---|---|
|
October 02, 2018 How to convert this function into a template ? | ||||
---|---|---|---|---|
| ||||
Hi all, I have a function and i want to convert this into a template so that i can use this function for more than one data type. This is my function. ```D void ArrayAdd( ref int[] x, int value) { int index = x.length ; x.length += 1 ; x[index] = value ; } ``` |
October 02, 2018 Re: How to convert this function into a template ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Vinod K Chandran | On Tuesday, October 2, 2018 5:40:18 AM MDT Vinod K Chandran via Digitalmars- d-learn wrote:
> Hi all,
> I have a function and i want to convert this into a template so
> that i can use this function for more than one data type.
> This is my function.
> ```D
> void ArrayAdd( ref int[] x, int value) {
> int index = x.length ;
> x.length += 1 ;
> x[index] = value ;
> }
> ```
Why do you have a function for that? All you need to do is use the append operator. e.g.
x ~= value;
- Jonathan M Davis
|
October 02, 2018 Re: How to convert this function into a template ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan M Davis | On Tuesday, 2 October 2018 at 11:49:06 UTC, Jonathan M Davis wrote:
> Why do you have a function for that? All you need to do is use the append operator. e.g.
>
> x ~= value;
>
> - Jonathan M Davis
Thanks for the reply. I did not find that it in documentation. Ofcourse i lost a chance to learn about templates. By translating a well known function into a template, i can easiy learn the concept, since the template documentation is little bit hard to understand.
|
October 02, 2018 Re: How to convert this function into a template ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Vinod K Chandran | On Tuesday, October 2, 2018 6:09:53 AM MDT Vinod K Chandran via Digitalmars- d-learn wrote: > On Tuesday, 2 October 2018 at 11:49:06 UTC, Jonathan M Davis > > wrote: > > Why do you have a function for that? All you need to do is use the append operator. e.g. > > > > x ~= value; > > > > - Jonathan M Davis > > Thanks for the reply. I did not find that it in documentation. Ofcourse i lost a chance to learn about templates. By translating a well known function into a template, i can easiy learn the concept, since the template documentation is little bit hard to understand. The template equivalent would have been something like void arrayAdd(T)(ref T[] x, T value) { auto index = x.length; x.length += 1; x[index] = value; } But if you're new to the language, I'd suggest that you read this site / book: http://ddili.org/ders/d.en/index.html - Jonathan M Davis |
October 02, 2018 Re: How to convert this function into a template ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan M Davis | On Tuesday, 2 October 2018 at 12:23:47 UTC, Jonathan M Davis wrote:
>
> The template equivalent would have been something like
>
> void arrayAdd(T)(ref T[] x, T value)
> {
> auto index = x.length;
> x.length += 1;
> x[index] = value;
> }
>
> But if you're new to the language, I'd suggest that you read this site / book:
>
> http://ddili.org/ders/d.en/index.html
>
> - Jonathan M Davis
Thanks a lot. Great help !. I will sure the check the link. :)
The doumentation did not tell me about the (T) in template. I can understand the "(T)" s inside the parentheses but i can't understand the one after template name.
|
October 02, 2018 Re: How to convert this function into a template ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Vinod K Chandran | On 10/02/2018 07:39 AM, Vinod K Chandran wrote: > Thanks a lot. Great help !. I will sure the check the link. :) I find the Index section useful (yes, can be improved). For example, just seach for "append" on this page: http://ddili.org/ders/d.en/ix.html > The doumentation did not tell me about the (T) in template. I can > understand the "(T)" s inside the parentheses but i can't understand the > one after template name. The Templates chapter says: Although T is an arbitrary name, it is an acronym for "type" and is very common in templates. http://ddili.org/ders/d.en/templates.html#ix_templates.function%20template Ali |
October 03, 2018 Re: How to convert this function into a template ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | On Tuesday, 2 October 2018 at 17:37:58 UTC, Ali Çehreli wrote:
> On 10/02/2018 07:39 AM, Vinod K Chandran wrote:
>
> > Thanks a lot. Great help !. I will sure the check the link. :)
>
> I find the Index section useful (yes, can be improved). For example, just seach for "append" on this page:
>
> http://ddili.org/ders/d.en/ix.html
>
> > The doumentation did not tell me about the (T) in template. I
> can
> > understand the "(T)" s inside the parentheses but i can't
> understand the
> > one after template name.
>
> The Templates chapter says:
>
> Although T is an arbitrary name, it is an acronym for "type" and
> is very common in templates.
>
>
> http://ddili.org/ders/d.en/templates.html#ix_templates.function%20template
>
> Ali
Thanks for the reply. Yes, the tutorial in that link contains append array. I think its good for me to learn with this tutorial, everything is structured and easy to find.
|
Copyright © 1999-2021 by the D Language Foundation