1. Types and classes. Objects Values
The definition of types and classes in the Lada language differ only in the keyword. Both types and classes can have properties, methods, events, and even operations (relations or predicates) can be defined for both the class and the type. Sometimes, it is even difficult to determine what type to create.
Example 1. Creating the Time class.
Class time
{
Dim Hour: Integer
Dim Minute: Integer
Dim Second: Integer
}
Example 2. Creating the type Time.
Type time
{
Dim Hour: Integer
Dim Minute: Integer
Dim Second: Integer
}
Both in types and in classes, Dim properties are determined by operators. And, just like in classes, we can say the Hour property of the type (or class) Time. But between them there is a significant difference in the fact that types create value objects, and classes create so-called reference objects to which you can refer and each object has its own personality. If for data defined as a type, individuality is not important, then for objects created by a class, this is a fundamental point. An object created by a class (for example, a Client) has its own personality and can be present in a single copy (and correspond only to one real client). Any object that accesses the Client object does this by reference or pointer. Thus, all objects that access the Client object can be sure that they are dealing with a single object. Changes made to the Client object will be available to all users of this object.
If we want to compare two objects created by a class, then we must make the identification of the objects themselves by reference or pointer. As a rule, copies of reference objects are not made. Is that if necessary replicate online or archiving. But in any case, if the copies are created, it is necessary to synchronize their updates. On the contrary, the comparison of two value objects occurs by comparing the properties of these objects, even if they are actually different objects and are in different memory areas.
The value of the value objects is in the value itself. At the same time there can be hundreds of objects with the same time value and they are all interchangeable. You can change the value, but then it will be a new object. On the contrary, changing one of the properties (for example, changing the position or salary of the Client object) in the reference object does not create a new (in the sense of another) object. Changes properties, but it is still the same object.
Applying the Dim operator to a type creates an object value (performing all the operations necessary to create an object). Applying the Dim operator to a class creates a reference to a class object and in no way performs any other actions to create the object itself. However, it is necessary to create an object and refer to it. Objects types and classes can be distinguished by color. By changing it, you can create an object using the Dim operator for classes, and vice versa create references to value objects. This is also necessary.
')
2. Operation Equality Relationship. Assignment.
Comparing values if they do not cause the same type of questions. The situation with objects generated by classes is quite different. Here we are not talking about comparison, but about identifying an object, and this identification is generally not determined by the equality of the values of all or a group of properties, but in some internal content of the object, which depends not only on the object itself, but also on our attitude to it and its significance. in the context in question. Omitting philosophical details, the implementation of this concept consists in checking the link to the object (object identification).
So, the equality relation operation is performed differently for values and for reference objects. For values, this is a check of an object for equality of properties, and for reference objects it is an identification of an object.
The same difference leads to different contents of the assignment operation. So objects-types when assigning copy values. Those. the value that identifies the identifier to the right of the equal sign is completely copied into the value that identifies the identifier to the left of the equals sign. A completely different picture when performing the operation of assigning reference objects, they copy not an object, but a link to it. Therefore, changes to properties in any of the class objects are available to all users of this object. Changing values in object types affect only the object being changed.
Another consequence of this difference is in the parameters passed to the default procedure. For default values, a value is passed, and for reference objects, an object reference is passed as a parameter. But this can be changed, by manipulating the color, you can transfer directly to the procedure a copy of the reference object or a reference to the value object.
3. Values in lexical analysis.
Since type objects have values, they are expressed in some form as decimal, binary, text-based, dotted digits, prefixes, suffixes, and the like. But this clearly affects the lexical parsing, because as a result of lexical parsing we get not the lexeme, but the object-value. It turns out that creating a new type, we create new values, the expression of which in text form should be analyzed as the value and value of the corresponding type. Yes, and definitely.
It would be difficult and responsible to define the vocabulary of the language with the definition of type, however, pay attention that with the inheritance of properties we inherit the rules of lexical parsing. Taking the dot character “.” As the default separator with the Time type definition in Example 2, we can create the next value of the Time type.
Example 3. Assigning a value of type Time.
Dim T: Time
T = 2.30.45
It is clear that it is not so easy to do and it will be necessary to distinguish value types with the same number of delimiters and here you just need to add suffixes (or prefixes). Then the translator is able to determine the unambiguous determination of the value of a new type.
The need to define values (creation of value objects) during lexical parsing (and it happens during typing) is caused by the fact that values as objects actually exist in the text from the moment we typed them. And to accompany the documents, these values can play a significant role. For example, approval dates, deadlines, amounts, comments, etc. not just typed text, but essential characteristics of the document, the analysis of which may be necessary not only during the creation of the document, but also during its movement, archiving and, in general, at any moment. Recall that in the Lada system, editing text, programs, charts, maps and plans is done in a universal environment (the language changes, the environment remains.).
Example 4. Creating Weight type with suffix T.
Type weight
{
Dim Ton: Integer
Dim Kilogram: Integer
Dim Gram: Integer
T
}
Example 5. Assigning a value of 253 kilograms, 26 grams of type Weight.
Dim W: Weight = 0.253.26T
There is only one trouble here. Since the definition of types affects lexical parsing, custom types must be defined in the space before the translation and even before the lexical analysis.
The essential point for lexical analysis is the presence of values before and after. Usually, if there is no value before a point, then it is assumed 0. Similarly, the absence of a value after the last point indicates that the value of the corresponding property is 0. However, if we inherit the type Integer, then in the lexical parsing of the type Integer there is no absence of digits. You can take something here by default.
The definition of the Weight type will be as follows.
Example 6. Creating a Weight type with suffix T, and defining default values.
Type weight
{
Dim Ton: Integer = 0
Dim Kilogram: Integer = 0
Dim Gram: Integer = 0
T
}
But you can define multiple syntaxes for the same type. This is done by grouping syntax variants with the Extract = 1 attribute.
Example 6. Creating Weight type with different syntax.
Type weight
{Extract = 1
{Dim Ton: Integer = 0
Dim Kilogram: Integer = 0
Dim Gram: Integer = 0
T
}
{
Dim Kilogram: Integer = 0
Dim Gram: Integer = 0
kg
}
{
Dim Gram: Integer = 0
gr
}
}
Then in the lexical analysis the following equal values will be recognized as the type of Weight.
Example 7. Equal values of type Weight.
0.2.0T, 2.0kg, .2.0T, .2.T, 2.kg, 2000gr
The last value will also be 2 kilograms, but the execution of operations (including comparison) with the new type must also be determined.
4. Aggregates.
By analogy with the type definition, you can assign properties to reference objects by accepting the agreement that the default separator for property values in a class is a semicolon “;”. We call such a construction not a value, but an aggregate (Aggregate).
For all the grammar of the aggregate on the definition of values, the definition of classes does not affect the lexical analysis. Parsing values (and possibly objects) and type matching occurs at the next translation stages. Firstly, because the execution of some actions with reference objects is a question of an already created document. After all, even the creation of a reference object occurs not when typing, but at least as a result of translation, but as a maximum during the program execution. Secondly, as elements of an aggregate, there may be not only values, but also objects whose class may not be defined until the execution of the aggregate and in general this object may not be created until the execution.
Example 6. Location class definition.
Class Location
{
Dim Top: Integer
Dim Left: Integer
}
Example 7. Assigning the values of the Top and Left properties to an object of the Location class by an aggregate.
Dim L: Location = 2; 2 'Assignment implementation.
5. Identity. Determinable comparisons.
Both for reference objects and for values, it is necessary to check not the equality of objects, but the equality of some properties or a group of properties. For example, color, position or size of objects (sometimes of different classes). This operation is called an identity (the sign "~"). For this operation, it is only necessary to specify the property list in parentheses, by which the identity is verified. For example, with the same Client object, sometimes we can identify an identity by first name, last name, middle name. In general, two different objects may be identical in growth, salary, and in rare cases, by name, surname, patronymic. Or we can compare two objects by color or position.
Example 3. Verification of the identity of values of type Time by the Hour property.
Dim T1, T2: Time
...
If T1 ~ T2 (Hour) Then