"Sputnik" help  
Sputnik Help
Function Rule Add To Class

Description

This demonstrates how to create a function and add it to a class at runtime..

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

Remarks

The Add To Class rule in Sputnik allows you to dynamically add functions to a class at runtime. It provides flexibility in extending the functionality of a class without modifying its original definition. You can use different approaches to add functions to a class, such as adding functions inside the class, adding functions outside the class, or dynamically adding functions using built-in functions like ClassAddFunc.

With the Add To Class rule, you can dynamically create classes and their functions, add or remove functions at runtime, and modify the behavior of classes as needed. This dynamic nature of Sputnik allows for powerful runtime customization and adaptability in your code.

Additionally, when adding functions to a class at runtime using the Add To Class rule in Sputnik, it provides a safe way to extend the functionality without directly modifying the original class. This approach ensures that the integrity of the base class is maintained while allowing for customizations and enhancements through added functions.

By dynamically adding functions to a class, you can tailor its behavior to specific requirements without the need to modify the original class code. This flexibility allows for safer code maintenance and promotes better code organization by separating the core functionality from additional customizations.

Furthermore, the ability to add functions to a class dynamically enables you to adapt and evolve your codebase more easily. You can introduce new features, modify existing behavior, or even remove functions as needed, all without impacting the underlying class structure.

Overall, the Add To Class rule in Sputnik provides a safe and flexible mechanism to extend classes at runtime, enabling greater customization and adaptability in your code while maintaining code integrity and modularity.

Related

User Functions
Embedded Functions

Example

Example of adding a function to a class:

Class Test
{
    Function Cat()
    {
        if (!FuncExists("Dog"))
        {
            Embedded "c" Function Dog()
            {
                say "Woof! from class->" . GetClassName();
            };
        }
        Dog();
    }
}
$a = new Test();
$a->Cat();
// PRINTS
// Woof! from class->test

This is useful for adding functions to your class if needed.

Here we will add a function to a class directly and without needing to be inside the class at all:

Class Test
{
    Function Cat()
    {
        Dog();
    }
}
$a = new Test();
Embedded "c:Test" Function Dog()
{
    say "Woof! from class->" . GetClassName();
};
$a->Cat();
// PRINTS
// Woof! from class->test

This is very useful if you wish to dynamically add functions to a class.

We can also add functions 100% dynamically like so:

Class Test
{
    Function Cat()
    {
        Dog();
    }
}
$a = new Test();
ClassAddFunc("Test", "Dog", Function() {say "Woof! from class->" . GetClassName();});
$a->Cat();
// PRINTS
// Woof! from class->test

From variable:

Class Test
{
    Function Cat()
    {
        Dog();
    }
}
$a = new Test();
$func = Function() {say "Woof! from class->" . GetClassName();};
ClassAddFunc("Test", "Dog", $func);
$a->Cat();
// PRINTS
// Woof! from class->test

From source code:

Class Test
{
    Function Cat()
    {
        Dog();
    }
}
$a = new Test();
$funcSrc = @"Function Dog() {say \"Woof! from class->\" . GetClassName();}";
ClassAddFunc("Test", $funcSrc);
$a->Cat();
// PRINTS
// Woof! from class->test

From ByteCode:

Class Test
{
    Function Cat()
    {
        Dog();
    }
}
$a = new Test();
$byteCode = CodeCompile(@"Function Dog() {say \"Woof! from class->\" . GetClassName();}");
ClassAddFunc("Test", $byteCode);
$a->Cat();
// PRINTS
// Woof! from class->test

Using this stuff you can dynamically create classes and their functions and add/remove them all at runtime since Sputnik is designed to be super dynamic like that.


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