| Thread overview | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
March 29, 2009 How to define templates | ||||
|---|---|---|---|---|
| ||||
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"));
}
| ||||
March 29, 2009 Re: How to define templates | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Steve Teale | 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. | |||
March 29, 2009 Re: How to define templates | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | 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?
| |||
March 29, 2009 Re: How to define templates | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Steve Teale | 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. | |||
March 29, 2009 Re: How to define templates | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | 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 ;=) ?
| |||
March 29, 2009 Re: How to define templates | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Steve Teale | 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.
| |||
March 29, 2009 Re: How to define templates | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Steve Teale | 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. | |||
March 29, 2009 Re: How to define templates | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Walter Bright | 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.
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply