Opened 6 years ago

Last modified 6 years ago

#5526 new defect

MetaModelica: loop over list<Integer>

Reported by: jens.schoenbohm@… Owned by: Lennart Ochel
Priority: normal Milestone: Future
Component: Code Generation Version: v1.14.0-dev-nightly
Keywords: Cc:

Description (last modified by Per Östlund)

Using an Integer from a list<Integer> as array index will give the wrong array component, if the list is part of a record.

Example:

uniontype Index
  record INDEX
    list<Integer> idx;
  end INDEX;
end Index;
  
uniontype Compo
  record COMPO
    array<Real> co;
  end COMPO;
end Compo;

Supposed we have a Compo called compo and an Index called index, the for loop gives wrong array components:

for k in index.idx loop
  print(String(k));
  r := compo.co[k];
end for;

The print command gives the correct number, but k as array index
seems to have twice the value of the list member. So if it exceeds the array bounds we will see an error otherwise r has a wrong value.

A known workaround: Assign k to an integer variable and use it instead of k or do an integer operation with k at the place where you use k. For instance r := compo.co[k+0].

Change History (5)

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

Does it work if you use arrayGet() instead of [] indexing (which is not part of MetaModelica 1.0)?

comment:2 by jens.schoenbohm@…, 6 years ago

arrayGet works fine.

comment:3 by Francesco Casella, 6 years ago

@jens, does this solve your problem? If so, please close the ticket.

comment:4 by Martin Sjölund, 6 years ago

It might solve the problem, but we still have bugs in the MetaModelica compiler. Probably open tickets for this particular problem though...

comment:5 by Per Östlund, 6 years ago

Description: modified (diff)

Fixed code markup.

Note: See TracTickets for help on using tickets.