## digitalmars.D.learn - Re: A tutorial on D templates

"DNewbie" <run3 myopera.com> writes:
```I can't understand it. Why would someone need template programming. What
problem does template solve?
--

D
```
Jan 13 2012
Peter Alexander <peter.alexander.au gmail.com> writes:
```On 13/01/12 10:48 PM, DNewbie wrote:
I can't understand it. Why would someone need template programming. What
problem does template solve?

Suppose you want to write a function to get the minimum of two integers.
It's easy:

int min(int a, int b)
{
return a < b ? a : b;
}

Suppose then you want to use it with floats. You now need to write
another function.

float min(float a, float b)
{
return a < b ? a : b;
}

Suppose you then want to use it with doubles, reals, complex numbers,
strings etc. etc.  You would quickly get tired of writing these
functions, and more importantly you would likely make mistakes at some
point.

Templates allow you to solve this problem by writing the function once
with placeholders for types:

T min(T)(T a, T b)
{
return a < b ? a : b;
}

This will work for ints, floats, doubles... Anything that has a <
operator will work.

There's much more you can do with templates, but that's the fundamental
problem that they solve.
```
Jan 13 2012
Philippe Sigaud <philippe.sigaud gmail.com> writes:
``` On 13/01/12 10:48 PM, DNewbie wrote:
I can't understand it. Why would someone need template programming. What
problem does template solve?

Well read on and see :-)

Peter:
Suppose you want to write a function to get the minimum of two integers.
It's easy:

Oh.. I see.
Thank you everybody.

And that's the first, more visible part of templates, a bit like
generics in Java.
Template are incomplete pieces of code with left-empty 'slots' which
you can fill at compile-time to decide what code will be compiled.
You can generate entire functions / class hierarchies or nicely
crafted-for-your-need code / pre-computed-by-the-compiler code with
templates.

Philippe
```
Jan 14 2012
=?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
```On 01/13/2012 02:48 PM, DNewbie wrote:
I can't understand it. Why would someone need template programming. What
problem does template solve?

Here is another resource that tries to answer that question:

http://ddili.org/ders/d.en/templates.html

"Parts of the source code may be left to the compiler to be filled in
until that part is actually used in the program."

Ali
```
Jan 13 2012
=?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
```On 01/14/2012 12:11 AM, Philippe Sigaud wrote:
On Sat, Jan 14, 2012 at 01:08, Ali Çehreli<acehreli yahoo.com>  wrote:

http://ddili.org/ders/d.en/templates.html

Hi Ali, I discovered you had a chapter on templates just a few days
ago.

That chapter is intentionally incomplete. I think function, struct, and
class templates and their uses with type template parameters are the
most common. (That's a C++ programmer talking. ;)) I've left the rest of
templates to a later chapter.

Thank you. Please ignore the Inglish ;) mistakes for now. It is
constantly being corrected by my editor (Ergin Güney) and I.

I think I'll add a ressources/further

Thank you. I will do the same. It will be easier if it gets a permanent
home, in addition to its github page (
https://github.com/PhilippeSigaud/D-templates-tutorial ).

Philippe

Ali
```
Jan 14 2012
=?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
```On 01/14/2012 07:49 AM, Philippe Sigaud wrote:

That's cool, because the D community is small enough and still
dispersed enough that we should link one another to bring it all
together.

And I've finally found time to create two links to your 'D Templates: A
Tutorial' from the following page

http://ddili.org/ders/d.en/templates.html

Ali

P.S. Going off-topic, the following chapters of 'Programming in D' have
been translated since the last announcement:

* Redirecting Standard Input and Output Streams
* Files
* auto and typeof
* Name Space
* The for Loop
* The Ternary Operator ?:
* Literals
* Formatted Output

http://ddili.org/ders/d.en/index.html
```
Jan 31 2012
Philippe Sigaud <philippe.sigaud gmail.com> writes:
```On Sat, Jan 14, 2012 at 01:08, Ali =C3=87ehreli <acehreli yahoo.com> wrote:

Here is another resource that tries to answer that question:

=C2=A0http://ddili.org/ders/d.en/templates.html

"Parts of the source code may be left to the compiler to be filled in unt=

that part is actually used in the program."

Hi Ali, I discovered you had a chapter on templates just a few days
ago. I'll go and read it. I think I'll add a ressources/further

Philippe
```
Jan 14 2012
Philippe Sigaud <philippe.sigaud gmail.com> writes:
```On Sat, Jan 14, 2012 at 15:56, Ali =C3=87ehreli <acehreli yahoo.com> wrote:
On 01/14/2012 12:11 AM, Philippe Sigaud wrote:
On Sat, Jan 14, 2012 at 01:08, Ali =C3=87ehreli<acehreli yahoo.com> =C2=

=C2=A0 http://ddili.org/ders/d.en/templates.html

Hi Ali, I discovered you had a chapter on templates just a few days
ago.

That chapter is intentionally incomplete. I think function, struct, and
class templates and their uses with type template parameters are the most
common. (That's a C++ programmer talking. ;)) I've left the rest of
templates to a later chapter.

I agree. Your goal is not exactly the same as mine: you intend to give
a view on the entire language, for beginners, whereas I intend my doc
to be a deep plunge into templates, as complete as possible.

Thank you. Please ignore the Inglish ;) mistakes for now. It is constantl=

being corrected by my editor (Ergin G=C3=BCney) and I.

What I see is quite readable :-)

I think I'll add a ressources/further

Thank you. I will do the same.

That's cool, because the D community is small enough and still
dispersed enough that we should link one another to bring it all
together.

It will be easier if it gets a permanent
home, in addition to its github page (
https://github.com/PhilippeSigaud/D-templates-tutorial ).

I have no permanent home for my D projects, nor do I intend to do. You
can link to the Github project, that's what people do. Or directly to
the pdf, if you wish.
```
Jan 14 2012
Philippe Sigaud <philippe.sigaud gmail.com> writes:
```On Tue, Jan 31, 2012 at 09:12, Ali =C3=87ehreli <acehreli yahoo.com> wrote:
On 01/14/2012 07:49 AM, Philippe Sigaud wrote:

That's cool, because the D community is small enough and still
dispersed enough that we should link one another to bring it all
together.

That's quite normal.

And I've finally found time to create two links to your 'D Templates: A
Tutorial' from the following page

=C2=A0http://ddili.org/ders/d.en/templates.html

And thank you too!

Ali

P.S. Going off-topic, the following chapters of 'Programming in D' have b=

translated since the last announcement:

* Redirecting Standard Input and Output Streams
* Files
* auto and typeof
* Name Space
* The for Loop
* The Ternary Operator ?:
* Literals
* Formatted Output