Dynamically concrete type parameter in generics

Hi all,

I have the following situation:
1. In my language it is possible to define parameters without providing type information explicitly. The type is inferred using by looking at the type of the initalization value. This works fine.
2. In my code generation these parameters shall be transformed to classes that extend a specific generic class. The type parameter should be the type of the parameter definition from my own language.

I started straight forward by defining a $INSERT$ macro and creating a new base class with a new type parameter node using the specific type information. But when I preview the generated text, I get an error message "broken reference". The target model of the new base class is null. Ok, this seems concequent since I created a new node in the nowhere.

Is there any (elegant) solution for that?

Thank you in advance,
LaAck
3 comments
Hi,

I guess you have something like the following in the $INSERT$ macro body:

<quotation new BaseClass< Type > (...) quotation>
prettyPrint();

The reference to `BaseClass` should be to the model that is persistent throughout the generation steps, such as a "runtime model". In other words, `BaseClass` should come from a library code. Please ensure that this is the case.
0
Hi,

thank you very much for your answer. Yes, I already created a class declaration node 'BaseClass' in a runtime solution of my target language as well as a class 'BaseClass<T>' that extends 'BaseClass'. In the $INSERT$ macro body I have something like

     "new BaseClass<Integer>"

I want to concrete the type parameter dependent on the mentioned inferred parameter type. I don't want to specify all possible type concretizations explicitly and in advance.
So currently I'm tyring to copy the BaseClass<T> node and replace the T by an instance of the concrete type. But this does not work since I'm not allowed to modify the "persistent model" you mentioned. Is it possible to solve this issue in any way?

Thanks again in advance and best regards,
LaAck
0
Normally you would write the `new` expression as a template, and put a $COPY_SRC$ macro on the type parameter, provided you have a separate mapping for your custom types to Java types, or a $MAP_SRC$, where you would substitute the correct types yourself.

So, I suggest you avoid using the $INSERT$ macro, it's really not supposed to be used in this way. Besides, it's much easier to use templates, and the generator takes care of all the references.
0

Please sign in to leave a comment.