Jump to page: 1 2
Thread overview
A tutorial on D templates
Jan 13, 2012
Philippe Sigaud
Jan 13, 2012
DNewbie
Jan 13, 2012
Peter Alexander
Jan 14, 2012
DNewbie
Jan 14, 2012
Philippe Sigaud
Jan 14, 2012
Ali Çehreli
Jan 14, 2012
Philippe Sigaud
Jan 14, 2012
Ali Çehreli
Jan 14, 2012
Philippe Sigaud
Jan 31, 2012
Ali Çehreli
Jan 31, 2012
Philippe Sigaud
Jan 14, 2012
Joel
Jan 16, 2012
Simen Kjærås
Jan 16, 2012
Philippe Sigaud
Jan 17, 2012
Simen Kjærås
January 13, 2012
[Cross-posted with D.announce, since it's also an annoucement]

Hello all,

I discovered D a few years ago and, seeing the recent increase in community projects, I looked for a way to bring my own small part to it.

I quite like D templates and wanted to try LaTeX again, so I decided to bite the bullet and wrote a tutorial on templates. It's far from finished and most probably full of mistakes but since it's already quite big, I need some inputs.

It's a Github project, here:

https://github.com/PhilippeSigaud/D-templates-tutorial

The resulting pdf is there:

https://github.com/PhilippeSigaud/D-templates-tutorial/blob/master/dtemplates.pdf

(click on View Raw)

If you have any comment, criticism, explanation, what have you, I'm game. What section should be expanded, what example would be cool, etc. If you see a mistake, do not hesitate to tell me: it's the first time I put thoughts on paper like this. Github issues management is far from perfect, but it's usable. Even better would be pull requests :)

There is an 'Examples' section where I show what can be done with templates and there I 'borrowed' some code posted here, with attribution. I already exchanged with Andrej Mitrovic (thanks!), but also took some code from Timon Gehr, Simen Kjaeraas, Trass3r and Jacob Carlborg. Guys, if any of you have a problem with that, tell me so and I'll take down the code of course. But if any of you could give me some explanation (a small paragraph or two?) about what your code does, I'll be forever grateful :)

This also extend to anyone who would want to share some template love/lore with the rest of us.

Philippe
January 13, 2012
I can't understand it. Why would someone need template programming. What problem does template solve?
-- 

  D
January 13, 2012
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.
January 14, 2012

On Fri, Jan 13, 2012, at 11:28 PM, Peter Alexander wrote:
> 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:

Oh.. I see.
Thank you everybody.
January 14, 2012
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
January 14, 2012
>> 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
January 14, 2012
On Sat, Jan 14, 2012 at 01:08, Ali Çehreli <acehreli@yahoo.com> wrote:

> 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."

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 reading part in my doc and put a link to your chapter.

Philippe
January 14, 2012
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.

> I'll go and read it.

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
> reading part in my doc and put a link to your chapter.

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

January 14, 2012
On Sat, Jan 14, 2012 at 15:56, Ali Çehreli <acehreli@yahoo.com> wrote:
> 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.

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.

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

What I see is quite readable :-)


>> I think I'll add a ressources/further
>> reading part in my doc and put a link to your chapter.
>
> 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.
January 14, 2012
Good work Philippe, looks good!

« First   ‹ Prev
1 2