Opened 7 years ago

Closed 7 years ago

#4579 closed defect (fixed)

Add support for MetaModelica array literals

Reported by: Per Östlund Owned by: Martin Sjölund
Priority: normal Milestone: 1.13.0
Component: MetaModelica Version: v1.13.0-dev-nightly
Keywords: Cc:

Description

MetaModelica currently doesn't have any support for array literals. You can fake it by using listArray, but it seems this isn't ideal since it generates code that calls listArray on list literals rather than generating actual array literals.

The new instantiation uses arrays quite a lot and is currently abusing listArray for array constants, so this is something we should probably consider fixing sometime.

Change History (5)

comment:1 by Martin Sjölund, 7 years ago

Isn't this a duplicate? You cannot have array literals. You could have vector literals (immutable arrays), or do you really mean you want to share the same array between several instances of some data type (a global mutable variable)?

comment:2 by Per Östlund, 7 years ago

I want immutable arrays, e.g. (not necessarily with this syntax):

constant array<Integer> c = [1, 2, 3];

comment:3 by Martin Sjölund, 7 years ago

Would something like this be acceptable?

package P
  constant array<Integer> arr = MetaModelica.Dangerous.listArrayLiteral({1,2,3});
end P;

model M
  function f
    input Integer i;
    output Integer o=1;
  algorithm
    for i in 1:arrayLength(P.arr) loop
      print(String(i) + ": " + String(arrayGet(P.arr, i))+"\n");
    end for;
    for i in P.arr loop
      print(String(i) +"\n");
    end for;
  end f;
  Real r;
algorithm
  r := f(integer(time)+1);
end M;

Note that the array is truly immutable and that trying to update any value in it will cause seg.fault.

The alternative would be to introduce yet another type (vector) and handle iteration and conversion between Modelica/MM arrays, lists, vectors, and ranges.

in reply to:  3 comment:4 by Per Östlund, 7 years ago

Replying to sjoelund.se:

Would something like this be acceptable?

Yes, that would be fine, I'm only looking to use it for constants. I'm fairly sure I haven't got any code in the new frontend that tries to update a constant, but if there is that code is wrong anyway.

comment:5 by Martin Sjölund, 7 years ago

Milestone: Future1.13.0
Resolution: fixed
Status: newclosed
Note: See TracTickets for help on using tickets.