"Sputnik" help  
Sputnik Help
Language Reference - Variables - Casting

Casting

Casting in Sputnik is very powerful and can even be used to cast as functions or classes ontop of everything you expect from languages like C/C#.

There are 2 ways to cast a value as something else the first:

say "The cast is : " . Int(777.42);
// PRINTS
// The cast is : 777
This will cause everything inside the Int() to be converted to a signed 32-bit integer.

The second way is similar to C and works like this:

say "The cast is : " . (int)777.42;
// PRINTS
// The cast is : 777

The "(int)value" type will only convert what it *touches* so if you want to convert a large expression you must cover with () example:

my $a = 11.6;
say "The cast is : " . (int)(777.42 + $a);
// PRINTS
// The cast is : 789
Casting is very useful since there will be times when you really need to be using only integers or floating points and by casting you will make sure that you do.

Its worth noting the actual "(type)$value" cast supports casting overloading on Classes where as the function "type(value)" does not.

A special cast is the ability to cast things as an array example:

printr( (array)"Hello World" );
// PRINTS
// Array
// (
//     [0] => Hello World
// )

You can also check if it is an array before casting it like:

my $a = "Hello World";
printr( $a ~~ Array ? $a : (array)$a );
// PRINTS
// Array
// (
//     [0] => Hello World
// )

Built in casts are:

(ascii)         // Returns a copy of a string but with all Unicode characters stripped
                // out of it. It will not even try cast the Unicode it will just ignore them.
                // This is equal to "RAW" mode in BinaryFromStr()
(array)         // Casts the variable as an Array
(binary)        // Casts the variable as Binary
(bin)           // Alias for (binary)
(raw)           // Casts a string as binary and binary as string (raw byte<->char encoding ignored)
(bool)          // Casts the variable as a Boolean
(boolean)       // Alias for (bool)
(char)          // Casts the variable as a Char
(byte)          // Casts the variable as a 8-bit unsigned integer
(sbyte)         // Casts the variable as a 8-bit signed integer
(int16)         // Casts the variable as a 16-bit signed integer
(short)         // Alias for (int16)
(int32)         // Casts the variable as a 32-bit signed integer
(int)           // Alias for (int32)
(integer)       // Alias for (int64) -- or the highest signed integer available
(int64)         // Casts the variable as a 64-bit signed integer
(long)          // Alias for (int64)
(uint16)        // Casts the variable as a 16-bit unsigned integer
(ushort)        // Alias for (uint16)
(uint32)        // Casts the variable as a 32-bit unsigned integer
(uint)          // Alias for (uint32)
(uinteger)      // Alias for (uint64) -- or the highest unsigned integer available
(uint64)        // Casts the variable as a 64-bit unsigned integer
(ulong)         // Alias for (uint64)
(intptr)        // Casts the variable as a signed system (memory) pointer
(ptr)           // Alias for (intptr)
(uintptr)       // Casts the variable as a unsigned system (memory) pointer
(uptr)          // Alias for (uintptr)
(float)         // Casts the variable as a 32-bit floating point number
(single)        // Alias for (float)
(double)        // Casts the variable as a 64-bit floating point number
(num)           // Casts the variable as whatever number it can be cast as or return its existing number
(number) // Casts the variable as whatever number it can be cast as or return its existing number
(string) // Casts the variable as a String (StringBuilder) // Casts the variable as a String Builder class (sb) // Alias for (StringBuilder) (object) // Casts the variable as a raw Object (obj) // Alias for (object) (class) // Casts the variable as a Class however it should be used on arrays! (count) // Alias for the Count() function but useable as a cast!

You can also use (~identifier) operator with a function name in the place of a cast name this will cause whatever comes after the cast (ie the casts target) to call that function as the first argument and return the result for example:

function countLetters($str)
{
    return StrLen($str);
}

$a = "Hello";
say (~countLetters)$a;
// PRINTS
// 5

This will work with any function even built in ones for example:

$a = "Hello";
say (~strlen)$a;
// PRINTS
// 5

You can also use (~~identifier) operator with a class name in the place of a cast name this will cause whatever comes after the cast (ie the casts target) to call that function as the first argument and return the result for example:

Class Testy
{
    Function Hello()
    {
        say "Hello!";
    }
    Function __FromCast()
    {
        say "Loading class from cast!";}
}
$a = "Hello";
((~~Testy)$a)->Hello();
// PRINTS
// Loading class from cast!
// Hello!

Be sure to check out __FromCast magic function.

You can also cast arrays as classes by default it uses the StdClass for example:

$a = array("Cat" => "Meow", "Dog" => "Woof");
$c = (class)$a;
printr $c;
// PRINTS
// stdclass Class
// (
//     [cat] => Meow
//     [dog] => Woof
// )

And you can cast them as a specific class like so:

Class test
{
    Function Test()
    {
        say "Cat does: $cat";
        say "Dog does: $dog";
    }
    Function __FromCast()
    {
        say "Loading class from cast!";
        return true; // true = ADD the array!
    }
}
$a = array("Cat" => "Meow", "Dog" => "Woof");
$c = (test)$a;
$c->Test();
printr $c;
// PRINTS
// Loading class from cast!
// Cat does: Meow
// Dog does: Woof
// test Class
// (
//     [cat] => Meow
//     [dog] => Woof
// )

This can be used to create classes in an alternative way.

You can also cast a class function like so:

my $word = MakeWord(77, 42);
say (~CU:HigherByte)$word;
say (~CU:LowerByte)$word;

Class CU
{
    Function HigherByte($w)
    {
        return ((byte)((uint64)($w) >> 8)) + 1;
    }
    Function LowerByte($w)
    {
        return ((byte)((uint64)($w) & 0xff)) + 1;
    }
}

// PRINTS
// 78
// 42

This is useful if you wish to setup a little library to do all kinds of conversions.


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