www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - class template as a type

reply Karl <asdf asdf.com> writes:
Hi,

I've been playing around with templates, i'm a newbie in this matter, i created
a class template and i want to use it as a type in functions and create arrays
of it. Any help please.

Code sample:

module hello;

template Test(alias T)
{
    class Test
    {
        this(){} int x; ...
    }
}

void Foo(Test t){}

static Test[] myarray;
Sep 18 2007
parent reply BCS <ao pathlink.com> writes:
Reply to Karl,

 Hi,
 
 I've been playing around with templates, i'm a newbie in this matter,
 i created a class template and i want to use it as a type in functions
 and create arrays of it. Any help please.
 
 Code sample:
 
 module hello;
 
 template Test(alias T)
 {
 class Test
 {
 this(){} int x; ...
 }
 }
 void Foo(Test t){}
 
 static Test[] myarray;
 

void Foo(Test!(somthingToAlias) { } static Test!(somthingToAlias)[] myarray; look for "Explicit Template Instantiation" in http://www.digitalmars.com/d/template.html
Sep 18 2007
parent reply Karl <asdf asdf.com> writes:
BCS Wrote:

 Reply to Karl,
 
 Hi,
 
 I've been playing around with templates, i'm a newbie in this matter,
 i created a class template and i want to use it as a type in functions
 and create arrays of it. Any help please.
 
 Code sample:
 
 module hello;
 
 template Test(alias T)
 {
 class Test
 {
 this(){} int x; ...
 }
 }
 void Foo(Test t){}
 
 static Test[] myarray;
 

void Foo(Test!(somthingToAlias) { } static Test!(somthingToAlias)[] myarray; look for "Explicit Template Instantiation" in http://www.digitalmars.com/d/template.html

Thanks for your reply. "somthingToAlias" could be anything right? Lets suppose foo.bar1 or foo.bar2. Then "static Test!(foo.bar1)[] myarray;" would create an array only for elements instantiated from foo.bar1 and i couldn't append an element instantiated from foo.bar2, am i wrong? 1) Then how could i create an array for generic instantiated elements? 2) "void Foo(Test!(somthingToAlias) { }", i think something is missing: - ")". - A variable name, (correct me if wrong): void Foo(Test!(somthingToAlias) sta){ } Thanks in advance.
Sep 18 2007
parent reply Frits van Bommel <fvbommel REMwOVExCAPSs.nl> writes:
Karl wrote:
 BCS Wrote:
 
 void Foo(Test!(somthingToAlias) { }
 static Test!(somthingToAlias)[] myarray;

 look for "Explicit Template Instantiation" in

 http://www.digitalmars.com/d/template.html

"somthingToAlias" could be anything right? Lets suppose foo.bar1 or foo.bar2. Then "static Test!(foo.bar1)[] myarray;" would create an array only for elements instantiated from foo.bar1 and i couldn't append an element instantiated from foo.bar2, am i wrong? 1) Then how could i create an array for generic instantiated elements?

You could give your template class a non-templated base function, and use that as the type of the array elements.
 2) "void Foo(Test!(somthingToAlias) { }", i think something is missing:
 
 - ")".

Yes, he seems to have made a typo there
 - A variable name, (correct me if wrong): void Foo(Test!(somthingToAlias) 
sta){ }

Technically the name is optional, but it does come in quite handy when you actually want to *use* the parameter in the function body :).
Sep 19 2007
parent reply Karl <asdf asdf.com> writes:
Frits van Bommel Wrote:

 Karl wrote:
 BCS Wrote:
 
 void Foo(Test!(somthingToAlias) { }
 static Test!(somthingToAlias)[] myarray;

 look for "Explicit Template Instantiation" in

 http://www.digitalmars.com/d/template.html

"somthingToAlias" could be anything right? Lets suppose foo.bar1 or foo.bar2. Then "static Test!(foo.bar1)[] myarray;" would create an array only for elements instantiated from foo.bar1 and i couldn't append an element instantiated from foo.bar2, am i wrong? 1) Then how could i create an array for generic instantiated elements?

You could give your template class a non-templated base function, and use that as the type of the array elements.
 2) "void Foo(Test!(somthingToAlias) { }", i think something is missing:
 
 - ")".

Yes, he seems to have made a typo there
 - A variable name, (correct me if wrong): void Foo(Test!(somthingToAlias) 
sta){ }

Technically the name is optional, but it does come in quite handy when you actually want to *use* the parameter in the function body :).

Thanks for your reply. "You could give your template class a non-templated base function, and use that as the type of the array elements." I didn't understood. A small example please. Thanks man.
Sep 19 2007
parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Karl" <asdf asdf.com> wrote in message 
news:fcsokc$2uhn$1 digitalmars.com...

 "You could give your template class a non-templated base function, and
 use that as the type of the array elements."

 I didn't understood. A small example please.

He meant "non-templated base *class*". So: class TestBase { // put common methods here } class Test(alias T) : TestBase { this() { } int x; ... } void Foo(TestBase t) {} static TestBase[] myArray; Remember that unlike Java or C# generics, D's templates are done all at compile time. This means that a Test!(int) is just as different from a Test!(float) as an int[] is from a float[].
Sep 19 2007
parent reply Frits van Bommel <fvbommel REMwOVExCAPSs.nl> writes:
Jarrett Billingsley wrote:
 "Karl" <asdf asdf.com> wrote in message 
 news:fcsokc$2uhn$1 digitalmars.com...
 
 "You could give your template class a non-templated base function, and
 use that as the type of the array elements."

 I didn't understood. A small example please.

He meant "non-templated base *class*". So:

Yes I did. I'm not quite sure how I managed to typo 'class' to 'function' though...
Sep 19 2007
parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Frits van Bommel" <fvbommel REMwOVExCAPSs.nl> wrote in message 
news:fct4r0$hl9$1 digitalmars.com...
 Yes I did. I'm not quite sure how I managed to typo 'class' to 'function' 
 though...

It's understandable. The keys are right next to each other.
Sep 20 2007
parent Chris Nicholson-Sauls <ibisbasenji gmail.com> writes:
Jarrett Billingsley wrote:
 "Frits van Bommel" <fvbommel REMwOVExCAPSs.nl> wrote in message 
 news:fct4r0$hl9$1 digitalmars.com...
 Yes I did. I'm not quite sure how I managed to typo 'class' to 'function' 
 though...

It's understandable. The keys are right next to each other.

You have a [class] key on your keyboard!? ...is that thing available on Amazon? -- Chris Nicholson-Sauls
Sep 20 2007