如何在创建实例之前在模型中重新声明包?

How to redeclare package in a model before making an instance?

我愿意

    M_type(redeclare package L=L2) m2_instance;

但不起作用。相反,我可以写

    model M2_type
       extends M_type(redeclare package L=L2);
    end M2_type;
    M2_type m2_instance;

这不是一个更短的方法来避免声明 M2_type 吗?

必须将重新声明修饰符移至实例名称。

M_type(redeclare package L=L2) m2_instance;  // wrong syntax
M_type m2_instance(redeclare package L=L2);  // correct

下面是一个小包,它在 Dymola 2021x 和 OpenModelica 1.16.2 中演示了一切并完美模拟。

package Redeclaration
  package L1
    constant Real x = 1;
  end L1;

  package L2
    constant Real x = 2;
  end L2;

  model M_type
    replaceable package L = L1;
    Real x = L.x;
  end M_type;
  
  model Example
    M_type m_instance(redeclare package L=L2);
  end Example;
end Redeclaration;