| 90 | == Assign to metarecord fields directly == |
| 91 | |
| 92 | MetaModelica uniontype records are a bit different than Modelica records: they are immutable, so you can't just assign to them. Especially since uniontypes may have several records. |
| 93 | It is possible to list all fields and create a new record. |
| 94 | But it is also possible to match a uniontype to become a metarecord (sadly not in assignments yet, though). |
| 95 | Using this, it is possible to use dot-notation to access a field (member) of the record, and also to assign to it (given that it is not a function input). |
| 96 | |
| 97 | The following pattern seems to work well, and produces code that is easy to maintain (adding a field to PROGRAM will not require you to update this code). |
| 98 | Assigning to two different fields will cause two allocations of the record though, so there is a cost of using this (for assigning to a single field, there is an optimized implementation that is faster than retrieving all the fields and calling the record constructor). |
| 99 | |
| 100 | {{{#!mo |
| 101 | function removeInnerDiffFiledClasses |
| 102 | input Absyn.Program inProgram; |
| 103 | output Absyn.Program p := inProgram; |
| 104 | algorithm |
| 105 | p := match p |
| 106 | case Absyn.PROGRAM() |
| 107 | equation |
| 108 | p.classes = List.map(p.classes, removeInnerDiffFiledClass); |
| 109 | then p; |
| 110 | end match; |
| 111 | end removeInnerDiffFiledClasses; |
| 112 | }}} |
| 113 | |