Hello Ed,
the deserialzer constructs the B instance (which
sets its own default) and sets the persisted value afterwards via the standard
setter or not if there wasn't any value persisted before. What is the problem
except of the unnecessarily assigned value in the constructor (assumed this is
not expensive)?
This is the behaviour of a default.
The same is true for the copier.
The problem is only when you set the value in a B
instance to its default value from A and then serializes it. In this case the
value is not persisted because it is equal to its default (from A). This is how
the serializer currently works. But this value, from A, is indeed different from
the "default" of B, so one wants that it is persisted. This can be achieved via
the OPTION_KEEP_DEFAULT_CONTENT of XMIResource.
Ciao, Michael
Guys,
Comments
below.
Michael Mühlberg wrote:
Hello John,
I would suggest to change the constructor of the generated impl class for B
and set the value there. Take into account that the attribute can be
unsettable. In this case you also have to change the generated change
tracking variable from which I currently don't know the name :)
No, don't ever populate instance in the constructor. You must start
values in their ground state because things like deserialization and the
copier expect that.
As far as I know there is no generator include template foreseen for
changing the generation of constructor code. Taking the original templates
and modifying them is problematic because in this case you have to merge all
future changes therein.
Yes, though the templates don't change much these
days.
I didn't find any way to overwrite defaults only via modeling, one has to
modify the generated code or the generation templates :(
Reference defaults are problematic.
Ciao, Michael
"John T.E. Timm" <johntimm@xxxxxxxxxx> wrote in message
news:9af675507e16f9247052dde7c18e7de5$1@xxxxxxxxxxxxxxx...
I have two classes A and B. B is a subclass of A. B inherits an attribute
>from A and I want to default the value of this attribute when B is
constructed.
A : EClass
attr : EAttribute
B -> A : EClass
attr := 'someDefaultValue' : EAttribute
What is the best way to set the default value of an inherited EAttribute
or EReference?
References don't support
defaults at all.
My guess is that I need to extend code generation to pick
up an annotation attached to an EClass and generate some initialization
code in the constructor.
It's a bad idea though
because such a thing will not behave like a proper default. It will
appear to be eIsSet true and will be serialized.
Does such an annotation already exist? Is there a
more automated way of accomplishing this?
Best avoid it entirely... Or have just an
operation on A with a name that is overloaded by the attribute in B...
Thanks,
JT