"Sputnik" help  
Sputnik Help
Language Reference - Classes
"options" Class <name>
{
<statements>
... } "options" Class <name> Inherits "listOfClasses"
{
<statements>
... }

Description

A class in Sputnik is just an object but it is designed to be very easy to use and provide a lot of power with its built in language level support.

Parameters

<name> The name of the class it can be made of alphanumeric characters and _ characters.
<inherits>

Optional; A list of classes to inherit. See Inheritance.

<statements> Optional; None or more statements to be executed by the class at creation time.
<options> Optional; You can add special rules to your function to tell it how to operate using the [] brackets before the word "Function".

Here is a list of the possible rules:

TYPE                   WHAT IT DOES
"r"                    Causes the class to REPLACE another class by the same name if it exists

Remarks

None.

Related

With Statement
Abstract Functions
Static Classes
Embedded Classes
Class Inheritance
Class Rule Replace
Magic Functions
Cast Overrides
Operator Overrides

Example

Heres a very minimal example of using classes to create "Cat" objects where each one contains it's lives:

// define a class called Cat
class Cat
{
    my $lives; // gives it a variable to store lives in
    // here is the constructor you setup to help making the class
    Function __Construct($l)
    {
        $lives = $l; // store the lives
    }
}

// make two cat with different lives
my $felix = new Cat(9);
my $fluffy = new Cat(4);

// print it out
say "Felix lives: " . $felix->$lives;
say "Fluffy lives: " . $fluffy->$lives;

// PRINTS
// Felix lives: 9
// Fluffy lives: 4

The same as above but using a code block { } which allows us to run code inside the class at initalization so we can actually use a StdClass instead of having to make a new one:

// make two cat with different lives
my $felix = new StdClass() { @this->$lives = 9; };
my $fluffy = new StdClass() { @this->$lives = 4; };

// print it out
say "Felix lives: " . $felix->$lives;
say "Fluffy lives: " . $fluffy->$lives;

// PRINTS
// Felix lives: 9
// Fluffy lives: 4

Same as above but defining the name inside the class too:

// make two cat with different lives
my $a = new StdClass() { @this->$name = "felix"; @this->$lives = 9; };
my $b = new StdClass() { @this->$name = "fluffy"; @this->$lives = 4; };

// print it out
printf("%s lives: %d\n", $a->$name, $a->$lives);
printf("%s lives: %d\n", $b->$name, $b->$lives);

// PRINTS
// Felix lives: 9
// Fluffy lives: 4

Classes use a special @this macro inside themselves it always contains a reference to itself for example:

class Cat
{
    my $name;
    Function __Construct($n)
    {
        $name = $n;
        say "Our name is: " . @this->$name;
        printr @this;
    }
}

my $felix = new Cat("Felix");
my $fluffy = new Cat("Fluffy");

// PRINTS
// Our name is: Felix
// cat Class
// (
//     [name] => Felix
// )
// Our name is: Fluffy
// cat Class
// (
//     [name] => Fluffy
// )

Ths @this macro only exists within the class itself.

In Sputnik core functions are always prioritized over class functions to get around this problem make sure to use the @this macro like so:

class test
{
    Function Clear()
    {
        say "Clear!";
    }
    Function testy()
    {
        @this->Clear();
    }
}

$a = new test();
$a->testy();

// PRINTS
// Clear!

If we didnt use the @this macro things would go very wrong observe:

class test
{
    Function Clear()
    {
        say "Clear!";
    }
    Function testy()
    {
        Clear();
    }
}

$a = new test();
$a->testy();

// PRINTS
// Exception (FunctionWrongParamCount):
// clear() requires 1 or 2 params

This is because without telling it to use the @this macro the class doesnt know if you want to run the core function or the class function. Of course with functions that dont share a name with Sputnik core functions you can just call it directly like so:

class test
{
    Function Cat()
    {
        say "Purr!!!";
    }
    Function testy()
    {
        Cat();
    }
}

$a = new test();
$a->testy();

// PRINTS
// Purr!!!

Function References

Using functions:

Function Bar()
{
    say "bar";
}

Class test
{
    Function Foo()
    {
        say "foo";
    }
}

my $bar = FuncRef(id bar);
my $foo = ClassFuncRef(id test:foo);

$bar();
$foo();
// PRINTS
// bar
// foo

Using &:

Function Bar()
{
    say "bar";
}

Class test
{
    Function Foo()
    {
        say "foo";
    }
}

my $bar = &bar;
my $foo = &test:foo;

$bar();
$foo();
// PRINTS
// bar
// foo

Magic Functions

Classes have a number of magic functions these can do special actions for example here is one of them:

class Cat
{
    my $name;
    Function __Construct($n)
    {
        $name = $n;
    }
    Function __Invoke($num)
    {
        say "$name meows $num time(s)";
    }
}

my $felix = new Cat("Felix");
my $fluffy = new Cat("Fluffy");
$felix(3);
$fluffy(5);

// PRINTS
// Felix meows 3 time(s)
// Fluffy meows 5 time(s)
Notice the __Invoke function? This is a magic function that causes the class variable to accept being used as a call like any other function.

Cast Overrides

Classes can use override casting to allow them to cast as almost anything this is most useful with the ToString cast for example:

class Test
{
    my $i;
    my $j;
    Function __Construct($i, $j)
    {
        @this->$i = $i;
        @this->$j = $j;
    }
    Function __ToString()
    {
        return "(I: $i, J: $j)";
    }
}

my $a = new Test(10, 20);
say (string)$a;

// PRINTS
// (I: 10, J: 20)

This is very useful in getting a quick print out on whats inside a class in a way the class creator wants it to be seen.

Operator Overrides

Classes can override operators to allow them take charge of how stuff like == (equate) is handled when used on a class.

class Test
{
    my $i;
    my $j;
    Function __Construct($i, $j)
    {
        @this->$i = $i;
        @this->$j = $j;
    }
    Function __OpEquate($x)
    {
        say "Compare == function is being run";
        return @this->$i == $x->$i && @this->$j == $x->$j;
    }
}

my $a = new Test(10, 20);
my $b = new Test(10, 22);

say "Compare with different numbers";
if ($a == $b)
{
    say "Same";
}
else
{
    say "Not Same";
}
say "Now making both same numbers then compare";
$b->$j = 20;
if ($a == $b)
{
    say "Same";
}
else
{
    say "Not Same";
}
You can override almost every operator in Sputnik so classes can be very powerful.

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