www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - How to define templates

reply Steve Teale <steve.teale britseyeview.com> writes:
The documentation says:

TemplateDeclaration:
	template TemplateIdentifier ( TemplateParameterList ) Constraint(opt)
		{ DeclDefs }

DeclDefs as defined where?

What should be the effect of the following?

import std.stdio;

template gnomeSaying(T, U, V, string s)
{
    writefln(s ~ " motherfucker");
}

void main()
{
   writefln(gnomeSaying!(int, double, int, "Yo"));
}
Mar 29 2009
next sibling parent reply Jarrett Billingsley <jarrett.billingsley gmail.com> writes:
On Sun, Mar 29, 2009 at 1:46 PM, Steve Teale
<steve.teale britseyeview.com> wrote:
 The documentation says:

 TemplateDeclaration:
 =A0 =A0 =A0 =A0template TemplateIdentifier ( TemplateParameterList ) Cons=

 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0{ DeclDefs }

 DeclDefs as defined where?

http://www.digitalmars.com/d/2.0/module.html
 What should be the effect of the following?

 import std.stdio;

 template gnomeSaying(T, U, V, string s)
 {
 =A0 =A0writefln(s ~ " motherfucker");
 }

An error, since a function call is not a DeclDef.
Mar 29 2009
parent reply Steve Teale <steve.teale britseyeview.com> writes:
Jarrett Billingsley Wrote:

 On Sun, Mar 29, 2009 at 1:46 PM, Steve Teale
 <steve.teale britseyeview.com> wrote:
 The documentation says:

 TemplateDeclaration:
        template TemplateIdentifier ( TemplateParameterList ) Constraint(opt)
                { DeclDefs }

 DeclDefs as defined where?

http://www.digitalmars.com/d/2.0/module.html
 What should be the effect of the following?

 import std.stdio;

 template gnomeSaying(T, U, V, string s)
 {
    writefln(s ~ " motherfucker");
 }

An error, since a function call is not a DeclDef.

OK, I didn't look there - perhaps a definition in 'Decalrations' would make thing easier. But then I wonder, since a plain old string is acceptable as a template parameter, why I can't use a statement that does not involve any of the other template arguments. Also, would it be reasonable for the compiler to issue a warning or error message to the effect that T, U, and V were never mentioned in the template body. What is the basic difference between Templates and Macros - the declarations thing is obviously crucial, but why?
Mar 29 2009
next sibling parent Steve Teale <steve.teale britseyeview.com> writes:
Jarrett Billingsley Wrote:

 On Sun, Mar 29, 2009 at 2:28 PM, Steve Teale
 <steve.teale britseyeview.com> wrote:
 OK, I didn't look there - perhaps a definition in 'Decalrations' would make
thing easier.

 But then I wonder, since a plain old string is acceptable as a template
parameter, why I can't use a statement that does not involve any of the other
template arguments.

Because... templates don't contain statements? I'm not sure what your question is, or what not using T, U, and V have to do with it.
 Also, would it be reasonable for the compiler to issue a warning or error
message to the effect that T, U, and V were never mentioned in the template
body.

Same idea as function arguments or locals. Just a QOI issue.
 What is the basic difference between Templates and Macros - the declarations
thing is obviously crucial, but why?

That can't really be answered until macros make their way into the language ;) but templates parametrize declarations, whereas macros parametrize arbitrary code. A parametrized type is something very different from arbitrary code.

That's a most useful answer, and some of it should be in the documentation. I had tended to think that templates were something more general that macros. Maybe that's why (like many others) I've never really understood them. It also accounts for most of the error messages I've got when trying to use templates. So will we get macros ;=) ?
Mar 29 2009
prev sibling parent Walter Bright <newshound1 digitalmars.com> writes:
Steve Teale wrote:
 Also, would it be reasonable for the compiler to issue a warning or
 error message to the effect that T, U, and V were never mentioned in
 the template body.

No, because the template signature may be conforming to an externally applied interface, and may simply not need the args.
 What is the basic difference between Templates and Macros - the
 declarations thing is obviously crucial, but why?

Macros manipulate text, templates manipulate syntax trees. They happen at very different stages in compilation.
Mar 29 2009
prev sibling next sibling parent Jarrett Billingsley <jarrett.billingsley gmail.com> writes:
On Sun, Mar 29, 2009 at 2:28 PM, Steve Teale
<steve.teale britseyeview.com> wrote:
 OK, I didn't look there - perhaps a definition in 'Decalrations' would make
thing easier.

 But then I wonder, since a plain old string is acceptable as a template
parameter, why I can't use a statement that does not involve any of the other
template arguments.

Because... templates don't contain statements? I'm not sure what your question is, or what not using T, U, and V have to do with it.
 Also, would it be reasonable for the compiler to issue a warning or error
message to the effect that T, U, and V were never mentioned in the template
body.

Same idea as function arguments or locals. Just a QOI issue.
 What is the basic difference between Templates and Macros - the declarations
thing is obviously crucial, but why?

That can't really be answered until macros make their way into the language ;) but templates parametrize declarations, whereas macros parametrize arbitrary code. A parametrized type is something very different from arbitrary code.
Mar 29 2009
prev sibling next sibling parent Jarrett Billingsley <jarrett.billingsley gmail.com> writes:
On Sun, Mar 29, 2009 at 3:01 PM, Steve Teale
<steve.teale britseyeview.com> wrote:
 So will we get macros ;=) ?

The 'macro' keyword is already reserved, and Walter showed them as a future feature at the D con in 2007. Unfortunately they won't be coming in D2, probably D3, at least at last report from Walter. For all we know they'll just magically appear in the next compiler release as a minor note in the changelog. That's usually how things happen.
Mar 29 2009
prev sibling parent Jarrett Billingsley <jarrett.billingsley gmail.com> writes:
On Sun, Mar 29, 2009 at 3:24 PM, Walter Bright
<newshound1 digitalmars.com> wrote:
 What is the basic difference between Templates and Macros - the
 declarations thing is obviously crucial, but why?

Macros manipulate text, templates manipulate syntax trees. They happen at very different stages in compilation.

Macros manipulate syntax trees. Useful ones do, anyway.
Mar 29 2009