How to use interface template? How to model interface template properly in D.
Jonathan M Davis
jmdavisProg at gmx.com
Fri May 27 00:14:48 PDT 2011
On 2011-05-26 23:48, Matthew Ong wrote:
> On 5/27/2011 2:32 PM, Jacob Carlborg wrote:
> > On 2011-05-27 07:55, Matthew Ong wrote:
> >
> >
> > In D the syntax for declaring a template and instantiate a template is
> > not the same. Have a look at the first example of
> > http://www.digitalmars.com/d/2.0/template.htm
> > If don't understand after reading that example please ask again, I don't
> > want to just give away the answer. You'll learn more by reading the
> > documentation and figuring it out by yourself.
>
> Hi Jacob,
>
> > In D the syntax for declaring a template and instantiate a template
>
> is not the same.
> I do understand that declaring is with () and instantiate is !().
> That is the reason that I am asking
> // ### or IS IT: DefType1!(T1) ??
> RetVal1(T1) obj = new RetVal1(T1)(); // ### or IS IT: new RetVal1!(T1)();
>
> I read that document. There are little but no practical model.
>
> Is there any such syntax being used within the probos lib?
struct S(T)
{
this(T val)
{
this.val = val;
}
T val;
}
auto s = S!(int)(42);
or if S were a class
auto s = new S!(int)(42);
The parens are optional when there's only one template argument, so it could
be S!int(42) and new S!int(42) instead.
There is no ! in the template definition, but you always use it when
instantiating, unless it's inferred (which can be done with functions but not
types). e.g.
T func(T)(T val)
{
return val + 2;
}
auto v = func!int(5);
auto w = func(5);
assert(v == w);
- Jonathan M Davis
More information about the Digitalmars-d-learn
mailing list