3
$\begingroup$

From an answer to a Modelica-related question on SO I learned about the possibility to overload operators in Modelica by using spezialized class operator record. Looking into the Modelica specs for Version 3.2.2 (Chapter 14) I take it that operator records are available in the language (at least) since 2013.

Unfortunately, trying the suggested example for time conversions in WSM Version 4.3 results in an error when validating the class:

Error: Only classes of type 'operator record' may contain elements of type 'operator function'; '==' was found in a class that has restriction 'operator record'.

Is it possible to define custom operator records in the System Modeler and how can this be done? (What about the WL - where I have not tried it yet?)

For completeness I include the example code posted on SO here:

operator record Time
  Integer s "Second";

  encapsulated operator 'constructor'
    import Time;

    function from_s
      input Integer s "Seconds";
      output Time t(s=s);
    algorithm 
    end from_s;

    function from_d
      input Integer d "Days";
      output Time t(s=d*24*3600);
    algorithm 
    end from_d;
  end 'constructor';

  encapsulated operator 'String' "Convert Time to string"
    import Time;

    function formated
      input Time t;
      input String format = "s" annotation(choices(choice="s" "seconds", choice="d" "days"));
      output String str;

    algorithm 
      if format == "d" then
        str :=String(t.s/24/3600);
      else
        str :=String(t.s);
      end if;
    end formated;
  end 'String';

  encapsulated operator function '==' "Compare time records"
    import Time;
    input Time t1;
    input Time t2;
    output Boolean result "= t1 == t2";
  algorithm 
    result := t1.s == t2.s;
  end '==';

end Time;

Note: I have posted a similar question on Wolfram Community.

$\endgroup$

1 Answer 1

0
$\begingroup$

To make this here self-contained I will shortly wrap up the answers I have gotten on Wolfram Community (see here):

Wolfram System Modeler will compile operator record classes without problems, but unfortunately does not offer them as a choice when creating a new class using the New Class dialog from the menu (as of Version 12.0). Nonetheless, one can create operator records by textual input in the Text View window.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.