digitalmars.D.learn - How to convert this function into a template ?
- Vinod K Chandran (11/11) Oct 02 2018 Hi all,
- Jonathan M Davis (6/17) Oct 02 2018 Why do you have a function for that? All you need to do is use the appen...
- Vinod K Chandran (7/11) Oct 02 2018 Thanks for the reply. I did not find that it in documentation.
- Jonathan M Davis (13/26) Oct 02 2018 The template equivalent would have been something like
- Vinod K Chandran (6/17) Oct 02 2018 Thanks a lot. Great help !. I will sure the check the link. :)
- =?UTF-8?Q?Ali_=c3=87ehreli?= (10/14) Oct 02 2018 I find the Index section useful (yes, can be improved). For example,
- Vinod K Chandran (4/20) Oct 02 2018 Thanks for the reply. Yes, the tutorial in that link contains
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 ;
}
```
Oct 02 2018
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
Oct 02 2018
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 DavisThanks 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.
Oct 02 2018
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: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 DavisWhy do you have a function for that? All you need to do is use the append operator. e.g. x ~= value; - Jonathan M DavisThanks 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.
Oct 02 2018
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.
Oct 02 2018
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.htmlThe 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
Oct 02 2018
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 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.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.htmlThe doumentation did not tell me about the (T) in template. Icanunderstand the "(T)" s inside the parentheses but i can'tunderstand theone 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
Oct 02 2018








Vinod K Chandran <kcvinu82 gmail.com>