ModelicaSpecification icon indicating copy to clipboard operation
ModelicaSpecification copied to clipboard

Least and greatest value of empty enumeration

Open henrikt-ma opened this issue 10 months ago • 3 comments

The grammar allows an enumeration to not have any literals (by making the enum-list optional), but why?

short-class-specifier :
   IDENT "=" base-prefix type-specifier [ array-subscripts ]
   [ class-modification ] description
   | IDENT "=" enumeration "(" ( [ enum-list ] | ":" ) ")" description

As long as it is allowed we have a problem with models like this, where the greatest value of the enumeration is needed:

model EmptyEnumeration
  type E = enumeration();
  parameter Integer n = 0;
  Integer k = Integer(min({ E(i) for i in 1:n }));
end EmptyEnumeration;

henrikt-ma avatar Feb 06 '25 22:02 henrikt-ma

OpenModelica doesn't follow the grammar here and gives a syntax error for the empty enumeration. But I guess that just confirms that there shouldn't be any issue with making enum-list required.

perost avatar Feb 06 '25 23:02 perost

As I recall one issue with enumerations was sub/super-typing, and there were some discussions about being able to add enumeration members later. That was then restricted to just using enumeration(:) as constraining type - and one guess would be that enumeration() was an early variant of that.

HansOlsson avatar Feb 07 '25 07:02 HansOlsson

I realized that with replaceable enumerations there might be a use-case:

  model M
    replaceable type E=enumeration(:);
    Real x[E](each start=0);
  equation 
    der(x)=ones(size(x,1))-x;
  end M;
  M m1(redeclare type E=enumeration());
  M m2(redeclare type E=enumeration(A, B));

For examples, if someone wants to have trace-products with names it might make sense to allow none as well.

For some reason Dymola generates 0 as minimum for the missing enumeration.

HansOlsson avatar Feb 12 '25 14:02 HansOlsson