Extending BaseLanguage with value objects

Since I'd like to have my language immutable class definitions, similar to Scala's case classes, I came up with the idea of value objects. I searched the forum, but didn't find an appropriate topic. My first idea was to extend ClassConcept, since I would like value objects to be used wherever ClassConcepts are applicable. Is there a possibility to have the attributes isFinal and visibility of a sub-class of ClassConcept to automatically have the values true and PublicVisibility respectively?

Regards,

Hannes

3 comments

You should use constraints to ensure the properties/children are of proper values and then node factories (the Actions aspect) to set them properly upon node construction.

0

Thanks for your advice. It helped me to better understand MPS. I tried to move on and encountered one more obstacle. Say, a value object should automatically generate getters to defined attributes, as in the screenshot.

Although I think I have a good understanding of the various language aspects, I cannot imagine a possible solution for generating getters on the fly, say, when the attributes are edited. Even worse, they would have to be removed again, when attributes are deleted.

Another problem I came across is the generation aspect of the language. My idea was to generate one private final field per attribute and create a constructor to initialize them. The next screenshot shows the concept definition of Attribute.

It references a ClassConcept and I would like to put the name of the ClassConcept where the "int" is located in the next screenshot.

I would have expected a property macro for node.classConcept.name, but it doesn't show up. Furthermore, I don't have an idea how to dynamically generate the constructor parameters.

I would appreciate further advice, since MPS really fascinates me.

Regards,

Hannes

0

Hi Hannes,

I think you do not need generating attribute getters "on-the-fly", you only need them generated in the resulting Java code. For referencing your attributes from other classes, you'll have to provide an AttributeReference concept, which will point to the actual Attribute, the value of which you want to read.

I suspect that your Attribute concept should be referring to a Type node, not a ClassConcept. There are many types that are not backed by a class in Java.

In the template, "int" represents a type of the field, which, if you check out FieldDeclararion concept, is a child node of the FieldDeclaration node, not a property. Thus a node macro, namely COPY_SRC, is needed, not a property macro. The query will then look something like "node.myType", which will replace the "int" node with the type that your Attribute points to.

Vaclav

 

0

Please sign in to leave a comment.