Trouble initializing a templated class

Vlad Levenfeld via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Jul 5 10:08:21 PDT 2014


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


More information about the Digitalmars-d-learn mailing list