How to convert this function into a template ?

Jonathan M Davis newsgroup.d at jmdavisprog.com
Tue Oct 2 12:23:47 UTC 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:
> > 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





More information about the Digitalmars-d-learn mailing list