Changes between Version 12 and Version 13 of WritingEfficientMetaModelica


Ignore:
Timestamp:
2014-11-03T11:07:24+01:00 (10 years ago)
Author:
sjoelund.se
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • WritingEfficientMetaModelica

    v12 v13  
    8888Try/else is implemented as syntactic sugar. It is executed as if it was the above matchcontinue statement, but is shorter to write.
    8989
     90== Assign to metarecord fields directly ==
     91
     92MetaModelica 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.
     93It is possible to list all fields and create a new record.
     94But it is also possible to match a uniontype to become a metarecord (sadly not in assignments yet, though).
     95Using 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
     97The 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).
     98Assigning 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
     101function removeInnerDiffFiledClasses
     102  input Absyn.Program inProgram;
     103  output Absyn.Program p := inProgram;
     104algorithm
     105  p := match p
     106    case Absyn.PROGRAM()
     107      equation
     108        p.classes = List.map(p.classes, removeInnerDiffFiledClass);
     109      then p;
     110  end match;
     111end removeInnerDiffFiledClasses;
     112}}}
     113
    90114== Avoid matchcontinue, use tail recursion ==
    91115`matchcontinue` is slow. Whenever possible, use `match` instead. This avoids calling `setjmp`.