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
next sibling 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
prev sibling parent reply Karl <asd asdf.com> writes:
Chris Nicholson-Sauls Wrote:

 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
Hehehehe. Guys, back into the subject. The only problem implementing a base class is that anyone can create a custom class inherited from the base class, override methods, skip all the base class constructors and then add it to an array or pass it to a function. Then there seems to be no way to check if this custom class implements the class template.
Sep 21 2007
parent reply Frits van Bommel <fvbommel REMwOVExCAPSs.nl> writes:
Karl wrote:
 Chris Nicholson-Sauls Wrote:
 
 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
Hehehehe. Guys, back into the subject. The only problem implementing a base class is that anyone can create a custom class inherited from the base class, override methods, skip all the base class constructors and then add it to an array or pass it to a function. Then there seems to be no way to check if this custom class implements the class template.
So you make the constructor(s) private and then only classes in the same module can inherit from it.
Sep 21 2007
parent Karl <asdf asdf.com> writes:
Frits van Bommel Wrote:

 Karl wrote:
 Chris Nicholson-Sauls Wrote:
 
 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
Hehehehe. Guys, back into the subject. The only problem implementing a base class is that anyone can create a custom class inherited from the base class, override methods, skip all the base class constructors and then add it to an array or pass it to a function. Then there seems to be no way to check if this custom class implements the class template.
So you make the constructor(s) private and then only classes in the same module can inherit from it.
WOW. Great idea, cool man, thanks for everything. That surely will work, i don't know why i didn't think about that before...i guess i'm not smart enough.
Sep 21 2007