"Sputnik" help  
Sputnik Help
using(<expressions>)
{
statements
...
} using(<expressions>)~;

Description

Executes the expressions then runs the code within the statements and finally automatically runs unset() on each expression in the expressions list (Basically cleaning up after you).

Parameters

expressions A list of expressions to execute separated by ,

Remarks

Using statements may be nested.

The Using Statement in Sputnik provides a convenient way to execute expressions, run code within a block, and automatically clean up resources by calling unset() on each expression in the list of expressions. It allows for a controlled and efficient management of resources within a specific scope.

When using the Using Statement, you can specify a list of expressions separated by commas. These expressions are executed before the code within the block is run. This allows you to set up necessary resources or perform any required pre-processing before proceeding with the code execution.

Once the expressions have been executed and the block of code is entered, you can freely utilize the expressions within the statements. This allows for seamless integration of the expressions into the code without the need for additional setup or cleanup operations.

Once the block of code within the Using Statement is completed, the resources associated with the expressions are automatically cleaned up. This is achieved by calling unset() on each expression, ensuring that any allocated memory or resources are properly released. This automatic cleanup eliminates the need for explicit cleanup operations and helps prevent resource leaks or memory-related issues.

The Using Statement can be nested, allowing for more complex resource management scenarios. You can stack multiple Using Statements, each with its own list of expressions, to manage different sets of resources within different scopes. This flexibility enables efficient resource utilization and ensures that resources are properly cleaned up at the appropriate scope level.

In addition to managing general resources, the Using Statement can be particularly useful when working with classes that implement destructors. By creating an instance of a class within a Using Statement, you ensure that the destructor of the class will be automatically called at the end of the block. This helps ensure proper cleanup and disposal of resources associated with the class instance.

Overall, the Using Statement in Sputnik offers a convenient and efficient way to manage resources within a specific scope. By automating the setup and cleanup processes, it helps streamline code execution and minimizes the risk of resource leaks or memory-related issues.

Related

None.

Example

Example on a simple string:

my $cat = "yyy"; // String exists
using($cat) // using will use the string
{
    say "str is: $cat"; // prints the string just fine
}
say "Final: $cat"; // nothing... the string no longer exists..
// PRINTS
// str is: yyy
// Final: 

Example of using it with a class that will be deleted after the using block ends:

Class Test
{
    my $value;
    Function __construct($v)
    {
        $value = $v;
        say "Constructed";
    }
    Function __destruct()
    {
        say "Destructed";
    }
}
using($newClass = new Test(77))
{
    say "Hello world!: " . $newClass->$value;
}
// PRINTS
// Constructed
// Hello world!: 77
// Destructed

// Immediately after the using block finishes
// the class gets unset() and its __destruct will be run.

You can also stack them example:

Class Test
{
    my $value;
    Function __construct($v)
    {
        $value = $v;
        say "Constructed";
    }
    Function __destruct()
    {
        say "Destructed";
    }
}
my $cat = "meow";
using($cat, $newClass = new Test(77), $newClass2 = new Test(100))
{
    say "Hello world!: " . $newClass->$value;
    say "Hello world!: " . $newClass2->$value;
    say "Cat: " . $cat;
}
say "Cat now? " . $cat;
// PRINTS
// Constructed
// Constructed
// Hello world!: 77
// Hello world!: 100
// Cat: meow
// Destructed
// Destructed
// Cat now?

Using the deferred using meaning it becomes active until the function ends without needing { } blocks:

Class Textus
{
    my $Str;
    Function __Construct()
    {
        say "Textus __Construct";
        $Str = "";
    }
    Function __Destruct()
    {
        say "Textus __Destruct";
    }
    Function Append(string_t $a)
    {
        say "Textus Appending $a";
        $Str .= $a;
    }
    Function __ToString() string_t
    {
        say "Textus __ToString";
        return clone($Str);
    }
}

Function Cat()
{
    using (my $sb = new Textus())~; // remains in effect until end of the function
    $sb->Append("Meow");
    $sb->Append("Meow");
    $sb->Append("Meow");
    return (string)$sb;
}

say Cat();
// PRINTS
// Textus __Construct
// Textus Appending Meow
// Textus Appending Meow
// Textus Appending Meow
// Textus __ToString
// Textus __Destruct
// MeowMeowMeow

 


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