ModelicaSpecification icon indicating copy to clipboard operation
ModelicaSpecification copied to clipboard

Binary operators on records

Open eshmoylova opened this issue 3 years ago • 3 comments

Can binary operators like + be applied to records? Trying to find some explanation for #3139 and I find that the whole explanation of whether binary operators can be used with records or not is not clear. In 3.4, it says:

Modelica supports five binary arithmetic operators that operate on any numerical type: [^,*,/,+,-]

I could not find the definition of "numerical type", but there is a definition of "numeric class" in 10.6:

The term numeric or numeric class is used below for a subtype of the Real or Integer type classes. The standard type coercion defined in section 10.6.13 applies.

It would be nice to have it closer to 3.4. Based on that, we can assume records are not numerical type and we should not be allowed to add two records. However, 6.7 allows to perform operations on type compatible subexpressions:

Certain expressions consist of an operator applied to two or more type compatible subexpressions (A and B), including binary operators, e.g. A + B, if-expressions, e.g. if x then A else B, and array expressions, e.g. {A, B}. The resulting type of the expression in case of two type compatible subexpressions A and B is defined as follows:

  • If A is a record expression B must also be a record expression with the same named elements. The type compatible expression is a record comprised of named elements that are compatible with the corresponding named elements of both A and B. In an array expression the two records may contain elements with different sizes, but apart from that they must be of compatible types. That generates a heterogenous array of records, see chapter 10.

So is it allowed to have A + B where A and B are type compatible records?

This statement later in 6.7 is also contradictory:

  • If A has an operator record base class then B must also have an operator record base class, and it must be the same, and otherwise neither A nor B may have an operator record base class. This is also the operator record base class for the expression e.g. for if (cond) then A else B.

From 14.5, according to item 3, a constructor operator (if exists) may be used to find the matching function.

eshmoylova avatar Mar 30 '22 18:03 eshmoylova

6.7 does not allow or disallow any operations on type compatible subexpressions, it only defines what type compatible means and gives some examples of contexts where expressions are required to be type compatible. It does not say that all the listed types must be allowed in contexts that require type compatible expressions.

I'm also not sure if 6.7 should even be used for binary operators, since the rule for arrays is obviously wrong for that (many binary operators allows mixing scalars and arrays and also arrays of different sizes).

perost avatar Mar 30 '22 19:03 perost

I would say that A+B shouldn't be allowed for type compatible records (that are not operator records), and we need to clarify a lot here.

HansOlsson avatar Apr 11 '22 08:04 HansOlsson

Seems agreement to forbid, might be left-over from pre-operator record time.

HansOlsson avatar Apr 26 '22 14:04 HansOlsson