Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
July 05, 2014 Trouble initializing a templated class | ||||
---|---|---|---|---|
| ||||
I'm going through Adam Wilson's talk 'C# to D' and I've gotten hung up by one of his examples regarding generic programming in D. Specifically, I'm trying to implement the code example found here: http://youtu.be/6_xdfSVRrKo?t=16m44s. I created a templateExp.d file that looks like this: public class BaseClass {} public class OtherClass : BaseClass {} class SomeClass(T : BaseClass) { public T[] values; public void add(T input) { values ~= input; } } void main() { auto sc = new SomeClass(); OtherClass oc1 = new OtherClass(); OtherClass oc2 = new OtherClass(); sc.add(oc1); sc.add(oc2); import std.stdio; writefln("value count", sc.values.length); } When I run the dmd compiler, I get this error: >dmd templateExp.d teamplteExp.d(12): Error: class teamplteExp.SomeClass(T : BaseClass) is used as a type How can I initialize this class correctly? |
July 05, 2014 Re: Trouble initializing a templated class | ||||
---|---|---|---|---|
| ||||
Posted in reply to quakkels | > When I run the dmd compiler, I get this error:
>
> >dmd templateExp.d
> teamplteExp.d(12): Error: class teamplteExp.SomeClass(T : BaseClass) is used as a type
>
This is actually:
templateExp.d(12): Error: class templateExp.SomeClass(T : BaseClass) is used as a type
|
July 05, 2014 Re: Trouble initializing a templated class | ||||
---|---|---|---|---|
| ||||
Posted in reply to quakkels | On Saturday, 5 July 2014 at 16:47:32 UTC, quakkels wrote:
> I'm going through Adam Wilson's talk 'C# to D' and I've gotten hung up by one of his examples regarding generic programming in D. Specifically, I'm trying to implement the code example found here: http://youtu.be/6_xdfSVRrKo?t=16m44s.
>
> I created a templateExp.d file that looks like this:
>
> public class BaseClass {}
> public class OtherClass : BaseClass {}
>
> class SomeClass(T : BaseClass)
> {
> public T[] values;
> public void add(T input) { values ~= input; }
> }
>
> void main()
> {
> auto sc = new SomeClass();
> OtherClass oc1 = new OtherClass();
> OtherClass oc2 = new OtherClass();
>
> sc.add(oc1);
> sc.add(oc2);
>
> import std.stdio;
> writefln("value count", sc.values.length);
> }
>
> When I run the dmd compiler, I get this error:
>
> >dmd templateExp.d
> teamplteExp.d(12): Error: class teamplteExp.SomeClass(T : BaseClass) is used as a type
>
> How can I initialize this class correctly?
try SomeClass (T): BaseClass
|
July 05, 2014 Re: Trouble initializing a templated class | ||||
---|---|---|---|---|
| ||||
Posted in reply to Vlad Levenfeld | > try SomeClass (T): BaseClass
Not sure which line you want me to change. I don't want SomeClass to inherit from BaseClass. Rather, I want T to be restricted to classes that inherit from BaseClass.
When I change `class SomeClass(T : BaseClass)` to `class SomeClass(T) : BaseClass` I still get the "class templateExp.SomeClass(T) is used as a type" error.
|
July 05, 2014 Re: Trouble initializing a templated class | ||||
---|---|---|---|---|
| ||||
Posted in reply to quakkels | On Saturday, 5 July 2014 at 17:17:03 UTC, quakkels wrote:
>> try SomeClass (T): BaseClass
>
> Not sure which line you want me to change. I don't want SomeClass to inherit from BaseClass. Rather, I want T to be restricted to classes that inherit from BaseClass.
>
> When I change `class SomeClass(T : BaseClass)` to `class SomeClass(T) : BaseClass` I still get the "class templateExp.SomeClass(T) is used as a type" error.
ah, sorry, I misunderstood. It looks like you need to change the lin
auto sc = new SomeClass ();
to
auto sc = new SomeClass!BaseClass ();
The compiler complains because SomeClass is a template when you call SomeClass() without !() template parameters. It only becomes a type once instantiated with parameters.
|
July 05, 2014 Re: Trouble initializing a templated class | ||||
---|---|---|---|---|
| ||||
Posted in reply to Vlad Levenfeld | > ah, sorry, I misunderstood. It looks like you need to change the lin
>
> auto sc = new SomeClass ();
>
> to
>
> auto sc = new SomeClass!BaseClass ();
>
> The compiler complains because SomeClass is a template when you call SomeClass() without !() template parameters. It only becomes a type once instantiated with parameters.
Thanks. That did it.
Here's my working program.
public class BaseClass {}
public class OtherClass : BaseClass {}
class SomeClass(T : BaseClass)
{
public T[] values;
public void add(T input) { values ~= input; }
}
void main()
{
auto sc = new SomeClass!BaseClass();
OtherClass oc1 = new OtherClass();
OtherClass oc2 = new OtherClass();
sc.add(oc1);
sc.add(oc2);
import std.stdio;
writefln("value count: %d", sc.values.length);
}
|
Copyright © 1999-2021 by the D Language Foundation