Skip to content

Navigation Menu

Sign in
Sign up

How to introduce Modelica.Constants in classes #3941

Answered by thorade
tobolar asked this question in Q&A
Discussion options

There are several possibilities how to introduce a constant from Modelica.Constants in a class. E.g. for pi, I have found following in MSL:

  • Definition of constant: constant Real pi = Modelica.Constants.pi; (or also constant SI.Angle pi = Modelica.Constants.pi;)
  • Qualified import: Import Modelica.Constants.pi;
  • Directly in equation, e.g. phi = 2 * Modelica.Constants.pi * w;

Is there a preferrable way having advantages over others?

You must be logged in to vote

One more variant:
final constant pi = Modelica.Constants.pi because otherwise the constant could be modified with a modifier from an extending class.

Replies: 2 comments 1 reply

Comment options

I would suggest avoiding the first one (new constant). I would prefer Modelica.Constants.pi (or .Modelica.Constants.pi); unless it is used a lot of times and the import would make it easier to reader. The problem with import is that you always have to check that it is imported in the current class.

You must be logged in to vote
1 reply
Comment options

tobolar Jan 24, 2022
Collaborator Author

The problem with import is that you always have to check that it is imported in the current class.

This is probably less problematic for common constants, compared to other classes. I mean when there is used pi somewhere in an equation, one can expect it could be an imported variable.

Comment options

One more variant:
final constant pi = Modelica.Constants.pi because otherwise the constant could be modified with a modifier from an extending class.

You must be logged in to vote
0 replies
Answer selected by hubertus65
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /