Java wildcards... in D

Voitech via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Jan 18 11:19:22 PST 2016


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 i had something like generics and this could be 
implemented like:

class Element<T> {
     T value;

     Element(T value){
     this.value=value;
     }

}

class Parser{

Collection<Element<?>> parse(String expression){

     Collection<Element<?>> retVal = new ArrayList<>();
     //"expression"  would be parser but for this example lets say 
we just create  new elements
     Element<Integer> e1= new Element<>();
     e1.value=3;
     Element<String> e2 = new Element<>();
     e2.value="+"
     retValue.add(e1);
     retValue.add(e2);
     ...

     return retValue;
     }

}

And then in other class not connected through module or template 
parameter or anything i could call it just:

Parser p = new Parser();
Collection<Element<?>>  elements=p.parse("(1+2+3)/6");

and then check weather (element.value instanceof Operator) or 
other stuff.

if i do something like this in D:

module calculator;
class Element(T) {
	T value;

	this(T value){
		this.value=value;
	}

}

and then:

	Element[] parse(string expression){
             Element[] returnSlice;
             ...
             //"expression"  would be parser but for this example  
lets say we just create  new elements
             Element!(int) e1 = new Element();
             e1.value=3;
             Element!(string) e2= new Element();
             e2.value="+";
             returnSlice~=e1;
             returnSlice~=e2;
             return returnSlice;
         }
I'm getting error:
class calculator.Element used as a type.

How to represent this class template parameter T of Element(T) as 
wildcard Element(?) ??

Cheers Voitech









More information about the Digitalmars-d-learn mailing list