"Sputnik" help  
Sputnik Help
Rule Return Defer

Description

Before reading this section make sure to read all User Functions and understand it.

This demonstrates how to create a function processes the return statement (return value) after processing the defer statements instead of before like normal.

Remarks

The Rule Return Defer, denoted by the "rd" prefix, introduces a unique behavior in function processing by altering the execution sequence of defer statements and the return value. In traditional function execution, the return value is processed before any defer statements, but with the Rule Return Defer, this sequence is reversed.

Purpose
The primary purpose of the Rule Return Defer is to enable developers to capture changes made in defer statements before finalizing the return value. This becomes particularly useful when modifications to variables or states within the function need to be reflected in the return result.

Implementation
To implement the Rule Return Defer, simply prefix the function declaration with "rd." This signals to the interpreter that the return value should be determined after the execution of any defer statements within the function.

Caution
While Rule Return Defer provides a powerful mechanism for altering return behavior, it should be used judiciously. Introducing this rule might lead to unexpected behavior, especially when existing code relies on the conventional order of return processing.

Workarounds
Without Rule Return Defer, developers can use references to achieve a similar result. However, this approach involves creating a pointer to the variable and dereferencing it both in the defer statement and the return statement.

Conclusion
The Rule Return Defer in Sputnik introduces a unique way of handling return values in functions, providing developers with greater flexibility in managing state changes before finalizing the return result. As with any language feature, it's essential to weigh the benefits against potential complexities and use it judiciously to maintain code reliability.

Related

User Functions

Example

Functions process the return value before any defer statements as shown here:

Function Cat()
{
    $a = 10;
    defer $a = 20;
    return $a;
}
say Cat();
// PRINTS
// 10

Notice it returns 10? Despite the defer setting it to 20. If we wanted to capture any changes to the value after the return statement we could toggle on the Rule Return Defer as shown here:

// rd = Rule Return Defer
"rd" Function Cat()
{
    $a = 10;
    defer $a = 20;
    return $a;
}
say Cat();
// PRINTS
// 20

Now it obtained the value correctly after it was modified in a defer statement.

There is a work around without this rule and that is to use references as shown here:

Function Cat()
{
    $a = 10;
    defer *(&$a) = 20;
    return &$a;
}
say *Cat();
// PRINTS
// 20
However that method is a bit finnicky as we had to make a pointer to the variable and dereference it twice once in the defer and once in the function call.

Contact
Cheryl (uberfox@hotmail.com)
Homepage
http://ubersoft.org