Accessing record parameters in Modelica without declaring a record instance

Viewed 116

In Modelica, I'm able to access the contents of a record instance like so:

model Unnamed1
  record Example
    parameter Real x = 5;
  end Example;

  Example ex;
  Real test;
equation 
  test = ex.x;
end Unnamed1;

However, I'd like to access the contents of the record without declaring an instance of the record, like so:

model Unnamed1
  record Example
    parameter Real x = 5;
  end Example;

  Real test;
equation 
  test = Example().x;
end Unnamed1;

...but this doesn't work. Is there some way to achieve what I'm trying to do?

2 Answers

This is not possible (even from a grammar point of view). The right hand side of dot (.) needs to be a class or component reference. You can only access constants/parameters in packages via the dot notation.

package X
  constant Real x = 1;
end X;

model M
  Real x = X.x
end M;

Yes, it is possible without having an actual instance in the model but it requires some extra code.

model Unnamed1
  record Example
    parameter Real x = 5;
  end Example;

  function getX
    input Example r;
    output Real x;
  algorithm 
    x:=r.x;
  end getX;

  Real test;
equation 
  test = getX(Example());
end Unnamed1;

(I'm aware that it is cheating by having the instance in the function, but....) Another option is

  for r in {Example()} loop
    test=r.x;
  end for;

(allowed according to https://github.com/modelica/ModelicaSpecification/issues/1521 )

Related