Skip to content

Clarification about external functions extending (pure) functions #3775

@casella

Description

@casella

Section 12.3 states:

External functions must be explicitly declared with pure or impure.

This requirement is reasonable and easy to understand if you declare the function once and for all. It is not entirely clear what it means when partial interface declarations are involved. Consider the following MWE

package PureExtends
  package BaseFunctions
    partial function f
      input Real x;
      output Real y;
    end f;
  end BaseFunctions;

  package FunctionsModelica
    extends BaseFunctions;
    redeclare function extends f
    algorithm
      y := 2*x;
    end f;
  end FunctionsModelica;

  package FunctionsExternal
    extends BaseFunctions;
    redeclare function extends f
      external "C";
    end f;
  end FunctionsExternal;
end PureExtends;

My interpretation is that BaseFunctions.f is not external, hence it is pure by default (if that is the intention, we should probably write that explicitly in the specification). Given that, all functions inheriting from it also inherit its purity (if that is the intention, we should probably write that a function extending another function also inherits its purity). Hence, it is not necessary to declare Functions.External.f explicitly as being pure; the purity is inherited from the base class.

Is this interpretation correct? Or should one write

  package FunctionsExternal
    extends BaseFunctions;
    redeclare pure function extends f
      external "C";
    end f;
  end FunctionsExternal;

This seems to me unnecessarily redundant. The intent of the base class to be pure is clear, and whomever uses is, e.g.

model M;
  replaceable package P = PureExtends.BaseFunctions;
  Real x = P.f(time);
  Real y = P.f(time)^2;
end M

is clearly assuming that P.f is a pure function and its double call will be factored out by a smart compiler. So, any implementation of that function as an external function must be pure, there is no choice about that.

If that's the case, can we make it clearer in the spec?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions