By using the keyword redeclare it is possible to modify the type and/or the prefixes and possibly the dimension sizes of a declared element. This kind of modification is called a redeclaration. In most cases a declaration that can be redeclared must include the prefix replaceable.
The element in a redeclaration must have a type that is either a subtype of the replaceable element in the modified class or a subtype of a constraining type. An example is shown below with the concepts of redeclaration.
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;
class MiniCircuit
replaceable discrete Resistor R1;
end MiniCircuit;
The redeclaration of the R1 element changes its type from Resistor to TempResistor and its prefix from discrete to parameter.
class MiniCircuitRed
MiniCircuit tempcircuit(redeclare parameter TempResistor R1);
end MiniCircuitRed;
The MiniCircuitRed class can be rewritten as the MiniCircuit2
class MiniCircuit2
parameter TempResistor R1;
end MiniCircuit2;
The next example illustrates a redeclaration used in an extends clause.
class RedA
parameter Real x;
end RedA;
class RedA2 // A2 is a subtype of A
parameter Real x = 3.14;
parameter Real y;
end RedA2;
class RedB
replaceable RedA a(x = 1);
end RedB;
The redeclaration redeclare RedA2 a(y=2) is used in an extends clause to replace the original declaration RedA a(x=1) from class RedB, but merging the original nested modifier (x=1) with the modifiers in the redeclaration to effectively get RedA2 a(x=1,y=2). The type of the redeclared field a in class RedB is changed from RedA to RedA2 in class RedB2 which is correct since RedA2 is a subtype of RedA.
class RedB2
extends RedB(redeclare RedA2 a(y = 2)); // The result after redeclaration is RedA2 a(x = 1, y = 2)
end RedB2;
The class RedB, is equivalent to the class B2expanded below.
class B2expanded
RedA2 a(x = 1, y = 2);
end B2expanded;
instantiateModel(RedB2)
instantiateModel(B2expanded)