Java wildcards... in D

Chris Wright via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Jan 18 13:15:51 PST 2016


On Mon, 18 Jan 2016 19:19:22 +0000, Voitech wrote:

> Hi. I'm trying to parse an simple string expression to something like
> Element array. Each Element can have a value of unknown type, which will
> be further combined and calculated to let say real/double/float value,
> no mather.

In Java, Element<T> is always Element<Object> behind the scenes, and 
there's only one type.

In D, Element(T) is a template that produces a type. Element!int and 
Element!Object are entirely different and, thanks to what templates allow 
you to do, the compiler can't assume any relationship between the two.

You have to establish that relationship manually by creating a base type:

abstract class BaseElement {
}

class Element(T) : BaseElement {
  T value;
}

Alternatively, if this is not appropriate to your usecase, you may want 
to look at std.variant, which explicitly implements value boxing.


More information about the Digitalmars-d-learn mailing list