List + generics

Dear Team,

by the AST Block statement (Block := " (Declaration | Statement)* ") , i want to use List + generics, but i could not pass the compiler, would you please tell me where my problem is?

Preconditions: there were already abstact class Statement and class Declaration in my Statement pakage

what should i do to fix the problem?

Or is there any other suggestions to do the blockstatement?


It would help to post the error message as Java usually tells you exactly whats wrong and sometimes even how to resolve it.

I guess, Java tells you:

Incorrect number of arguments for type List<E>; it cannot be parameterized with arguments <Declaration, Statement>

(or some similar words)
which tells you what the problem is:
You gave an incorrect number of generic arguments:
The list wants one body type but you provided two.

yes, that was the error…but i want to have two different Type (Declaration and Statement) for Block…should I just create two Arrays with these two types as properties for my BlockStatement class? But how can i insure the order in the toString() method if there are twoi arrays? Any hint?

or the oder for Declaration and Statement in the block can be ignored?

It depends on what exactly you try to achieve (abstractly speaking):
(I switches the types to strings and ints for a more theoretical explanation)

If you want a list that can contain integers as well as strings, you either need a union type (or representation thereof) or a custom class hierarchy/embedding to represent this mixture typing.

If your class contains strings and separated (no interconnection for instance for order) integers, you probably want two storage container fields in your class.
One for strings and one for integers.

For own classes: If you do not care directly for the distinction, you could redesign the hierarchy and inheritances to group them under one common supertype (do not use Object). You then would probably delegate the distinction to the classes themselves whenever necessary.

To preserve order the approach with the two separate containers does not work directly. You could make it work using an additional data structure to store the order information but there might be a better way using the other approaches.

Note that the ASTFactory expects you to have a “DeclarationStatement” there anyway, so your efforts will most likely be unnecessary…