A declared element of a class must have the prefix replaceable in order to be redeclared, apart from redeclarations that only change the attribute value and/or restricts the variability and possibly the dimension sizes. The main motivation for this rule is that a class needs to be designed for inheritance that includes redeclaration. Otherwise unexpected errors may occur.
class MicroCircuit
replaceable Resistor R1 (R = 4.0);
end MicroCircuit;
type Voltage = Real(unit = "V");
type Current = Real(unit = "A");
connector Pin
Voltage v;
flow Current i;
end Pin;
model Resistor
Pin p, n "Positive and Negative Pins";
Voltage v;
Current i;
parameter Real R(unit = "Ohm") "Resistance";
equation
v = R*i;
end Resistor;
model TempResistor "Temperature dependent Resistor"
Pin p, n "Positive and Negative Pins";
Voltage v;
Current i;
parameter Real R(unit = "Ohm") "Resistance at reference Temperature";
parameter Real RT(unit = "Ohm/degC") = 0 "Temperature dependent Resistance";
parameter Real Tref(unit = "degC") = 20 "Reference Temperature";
Real Temp = 20 "Actual Temperature";
equation
v = i*(R + RT*(Temp - Tref));
end TempResistor;
model SuperTempResistor "Temperature dependent Resistor"
Pin p, n "Positive and Negative Pins";
Voltage v;
Current i;
parameter Real R(unit = "Ohm") "Resistance at reference Temperature";
parameter Real RT(unit = "Ohm/degC") = 0 "Temperature dependent Resistance";
parameter Real Tref(unit = "degC") = 20 "Reference Temperature";
Real Temp = 20 "Actual Temperature";
parameter Boolean Super = true;
equation
v = i*(R + RT*(Temp - Tref));
end SuperTempResistor;
The redeclare prefix is used at the front of a modifier containing a new variable or type declaration that replaces the original variable or type declaration element in the modified class.The reason for requiring a special keyword like redeclare is to reduce the risk for accidental programming errors that otherwise might occur when whole declarations are replaced by declarations of different types.
class TempMicroCiruit = MicroCircuit(redeclare TempResistor R1);
In order to allow further redeclaration of a redeclared element the replaceable prefix should be present also in the modifier.
class TempMicroCiruit2 = MicroCircuit(redeclare replaceable TempResistor R1);
In the class SuperTempMicroCircuit the keyword replaceable is left out, which means that no further redeclaration is possible.
class SuperTempMicroCiruit = TempMicroCiruit2(redeclare SuperTempResistor R1);
instantiateModel(MicroCircuit)
instantiateModel(TempMicroCiruit2)
instantiateModel(SuperTempMicroCiruit)