0

I have a question regarding UML DataTypes.

The UML Superstructure says:

A data type is a type whose instances are identified only by their value.

If I understand this correctly it means, equality is checked by looking at the values of all properties.

Does this also imply that DataTypes are always copied around like C# structs?

Are they passed by value to methods and if I assign a DataType to a new variable does this make a copy? (for example in Alf)

I could not find the answer to this on the internet.

jsf
  • 913
  • 3
  • 14
  • 22
  • By that definition, a `string` is a DataType, but a `string` is also a reference type. Basically, the UML "data type" is a semantic definition which doesn't prescribe a particular implementation (i.e. it doesn't say whether to use a reference type or a value type, in c#) – Matthew Watson Jan 14 '15 at 10:23
  • Well if you put that as an answer I would accept it. Basically the only difference between DataTypes and Classes is their equality behaviour. This is kinda awkward as the primitive types are DataTypes and I expect an Integer to be copied when assigned to a new variable. – jsf Jan 14 '15 at 12:06

2 Answers2

3

This is a good question. As is often the case, the semantic description in the base UML specification is a bit vague. However, Alf is based on Foundational UML (fUML) semantics, and fUML is precise on this point.

The short answer is that data types are, indeed, passed by value, not reference and are immutable. Any apparently mutating operation on a value of a data type actually results in the creation of a new value. For example, consider the following Alf code for the Point data type:

a = new Point(1,1);
b = a;
a.x = 2;

The assignment b = a; actually copies the point data value. The assignment a.x = 2; thus has no effect on b. Indeed, this second assignment is actually equivalent to a = new Point(2, a.y);. That is, it results in the creation of a new point data value that is then re-assigned to a.

These semantics are explicitly covered in the Alf and fUML specifications. The definition of data types is covered in subclause 10.4.4 of the Alf spec 1. However, the semantics of assignment per above is in subclause 8.8, where it says, under the description of a simple assignment to a property reference (like a.x):

If the property reference has a primary expression that is a local name or parameter name and has a type that is a structured data type, then the assignment to the property reference effectively assigns a new data value to that local or parameter name, with the given property updated.

If you are interested in further details on the semantics of data values, you can find this in the fUML spec 2. The semantics of values are discussed in subclause 8.3.2.1. The key thing is that there is a distinction made between data values and "extensional values". Extensional values exist in the extent of their classifiers at an execution locus independent of their use -- this is the equivalent in fUML of the runtime implementation concept of a "heap".

Extensional values include links, which are instances of associations, and objects, which are instances of classes. Further, there are reference values that are used to refer to objects. It is always references to objects that are passed around, not the object itself -- the object simply exists at the execution locus ("on the heap"). Thus, objects have reference semantics, as opposed to data values, which can be themselves be passed around as values.

There is thus no vagueness on the semantics of data values vs. objects in the fUML spec.

Ed Seidewitz
  • 101
  • 2
0

UML datatypes are more or less equal to the concept of Immutable Objects: an object that cannot be changed.

This is indeed also much like a Value Types in C# with the exception of things like strings, who are in fact Immutable Reference types. So strings would be considered UML datatypes as well.

Geert Bellekens
  • 12,788
  • 2
  • 23
  • 50
  • I could not find an official statement that DataTypes are immutable. Maybe they are meant to be immutable. But as long as it is not explicitly stated people will change properties of compound DataTypes for example. I think it is not safe to assume that DataTypes are immutable unless there is an official statement. – jsf Jan 14 '15 at 12:46
  • The fact that they are immutable can be derived from the fact that they can only be identified by their value. If the value changes you have another identity ergo another object. All objects with the same value have the same identity -> are the same object. – Geert Bellekens Jan 14 '15 at 13:00
  • I understand your point. However it bothers me that this is not made explicit in the standards. Probably it doesn't matter for UML. But I am using Alf which needs to have precise execution semantics. And I think in this case they are missing. The question is: If I have for example the following Alf code where `Point` is a DataType, what is stored in `b`: `a = b = new Point(1,1); a.x++;` – jsf Jan 14 '15 at 13:55
  • Seeing [this](http://stackoverflow.com/questions/17359144/the-semantic-of-data-type-in-uml) I think I got it now. No identity means that they are no objects. They do not reside on the heap as identifiable objects with an address. This implies that variables are not references to these types but need to have the whole datatype object stored in them. – jsf Jan 14 '15 at 14:02