Java wildcards... in D

2016-01-18 Thread Voitech via Digitalmars-d-learn
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 value;

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

}

class Parser{

Collection parse(String expression){

Collection retVal = new ArrayList<>();
//"expression"  would be parser but for this example lets say 
we just create  new elements

Element e1= new Element<>();
e1.value=3;
Element 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  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









Re: Java wildcards... in D

2016-01-18 Thread Ali Çehreli via Digitalmars-d-learn

On 01/18/2016 02:08 PM, Voitech wrote:

> alias Element =Algebraic!(real,string);
>
> i will get:
>
> Cannot store a int in a VariantN!(16LU, real, string)

Are you really storing a 'real' or a 'string'? (The default floating 
type in D is double, not real.) The following compiles and works as 
expected with dmd v2.069.2:


import std.variant;

void main() {
alias Element =Algebraic!(real,string);
auto r = Element(1.2L);
auto s = Element("hello");
}

Ali



Re: Java wildcards... in D

2016-01-18 Thread Voitech via Digitalmars-d-learn

On Monday, 18 January 2016 at 21:15:51 UTC, Chris Wright wrote:

On Mon, 18 Jan 2016 19:19:22 +, 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 is always Element 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.


Thank you for answering. I think i will use std.variant.Algebraic 
implementation for my needs. But about Algebraic... why first 
template parameter of it must be always int. Let say i want to 
store only real and string in Algebraic no int.

if i try to do something like :

alias Element =Algebraic!(real,string);

i will get:

Cannot store a int in a VariantN!(16LU, real, string)

if i declare as:

alias Element =Algebraic!(int,real,string);

this will be ok.

But anything other than Algebraic!(int,x,y,z...) doesn't compile, 
like:


alias Element =Algebraic!(uint,bool,string); //compilation error
alias Element =Algebraic!(ulong,Object,bool); //compilation error
alias Element =Algebraic!(long,real,string); //compilation error

Why is that ?


Cheers Voitech.


Re: Java wildcards... in D

2016-01-18 Thread Chris Wright via Digitalmars-d-learn
On Mon, 18 Jan 2016 19:19:22 +, 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 is always Element 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.