Trouble with SList for generic Stack class

H. S. Teoh via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Jun 2 16:18:44 PDT 2017


On Fri, Jun 02, 2017 at 11:05:43PM +0000, Mark via Digitalmars-d-learn wrote:
[...]
> Stefan, what do you mean that it must be a template?
> 
> Am I supposed to write template Struct(T) { class Stack { ... } }?

No, he means to turn class Stack into a template by adding a template
parameter to it, like I suggested.

In D, writing `class Stack(T) { ... }` is equivalent to writing:

	template Stack(T) {
		class Stack { ... }
	}

It's the so-called eponymous template.

The reason you need a template here is because, unlike Java, D does not
do type erasure to achieve generic types. Full type information is
preserved, so Stack!int and Stack!string represent distinct,
incompatible types.


T

-- 
Never wrestle a pig. You both get covered in mud, and the pig likes it.


More information about the Digitalmars-d-learn mailing list