Skip to content

Implement functions in pgm #1269

@Pablete1234

Description

@Pablete1234

Such function, would be a way to support locally scoped variables and have a return result. it could look something like this:

<variables>
  <variable id="thing" scope="match" default="5"/>
  <variable id="result" scope="match"/>
</variables>
<actions>
  <!-- Defines a get-avg function, that takes in params `a` and `b`, with a local `tmp` variable, and a `res` result variable -->
  <function id="get-avg" param="a" param="b" local="tmp" result="res" scope="match">
    <set var="tmp" value="a" />
    <set var="tmp" value="tmp + b" />
    <set var="res" value="tmp/2" />
  </function>

  <call id="comp-avg" fn="get-avg" param="1" param="5" var="result"/>
</actions>

Note: the example is intentionally verbose, that comp-avg call could all be just a <set var="result" value="(1+5)/2"/>, and the get-avg function uses a tmp that it does not need at all.

Also note, while calls can be nested inside other actions, functions should remain always top-level definitions as they do nothing but just define a set of tasks by themselves, they have to be called to actually do anything, and can't be directly used as an action for a trigger or similar.

They are not to be actions at code level, because they have different semantics, they require parameters passed in, something being done with the result, etc. You can't just make a trigger take in an action="get-avg" because it'd make no sense. You'd make a trigger take comp-avg which is a call action, that calls the function with certain params and stores the result. This is similar to how a variable isn't an action, but set action can do things with the variable.

This is not an easy task, it's pretty challening to implement a new scope or variables (local function variables) and have them work with other expressions, but it's something that could help the current status-quo of needing everything to be a global variable due to the lack of a local scope.

Metadata

Metadata

Assignees

No one assigned

    Labels

    discussionJust talkingfeatureNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions