digitalmars.D.learn - Java wildcards... in D
- Voitech (60/60) Jan 18 2016 Hi. I'm trying to parse an simple string expression to something
-
Chris Wright
(14/18)
Jan 18 2016
In Java, Element
is always Element - Voitech (19/40) Jan 18 2016 Thank you for answering. I think i will use std.variant.Algebraic
- =?UTF-8?Q?Ali_=c3=87ehreli?= (11/14) Jan 18 2016 Are you really storing a 'real' or a 'string'? (The default floating
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
Jan 18 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.
Jan 18 2016
On Monday, 18 January 2016 at 21:15:51 UTC, Chris Wright wrote:On Mon, 18 Jan 2016 19:19:22 +0000, Voitech wrote: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.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.
Jan 18 2016
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
Jan 18 2016