"Sputnik" help  
Sputnik Help
Parameter Types

Description

This demonstrates how to create a function that can use defined types with it's parameters.

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

Parameter types allow you to specify the expected data type of a function parameter. By placing the identifier (name of a type) before the variable name in a function parameter, you can enforce the conversion of any data received on that parameter to the specified type. It is important to note that if the conversion fails, Sputnik will assign a default value of the specified type, usually 0 for integer types, to the parameter.

Typedefs can be used as function arguments, allowing you to conveniently pass data in a structured format. When a typedef is used as a function argument, the system automatically converts the provided data into the corresponding typedef format.

Remarks

Related

User Functions, TypeDefs, Structs, Return Types

Example

Making an argument require strings:

printr Testy('A');
printr Testy(100);
printr Testy(100.0);
printr Testy(100.0f);

Function Testy(string_t $p)
{
    print "Testy got: " . vardump($p)->>TrimEnd();
}

// PRINTS
// Testy got: string(1) "A"
// Testy got: string(3) "100"
// Testy got: string(3) "100"
// Testy got: string(3) "100"

In this example, the Testy function utilizes the string_t parameter type (TypeDef). This type ensures that any value passed to the function is automatically converted to a string if it is not already a string.

An argument that only accepts Int64:

printr Testy('A');
printr Testy(100);
printr Testy(100.88);
printr Testy(100.777f);

Function Testy(int64_t $p)
{
    print "Testy got: " . vardump($p)->>TrimEnd();
}

// PRINTS
// Testy got: int64(65)
// Testy got: int64(100)
// Testy got: int64(100)
// Testy got: int64(100)

The Testy function takes an argument of type int64_t and prints the value of the argument after casting it to an int64 type. It demonstrates how different data types, such as characters, integers, and floating-point numbers, can be passed to the function and implicitly converted to the specified type.

An argument that only accepts an Arrays of Int64 values:

printr Testy(777);
printr Testy(array('A', 'B', 'C'));
printr Testy(array("10", "20", "30"));
printr Testy(array(10, 20, 30));
printr Testy(array(10.0, 20.0, 30.0));

Function Testy(int64_list_t $p)
{
    print "Testy got: " . vardump($p)->>TrimEnd();
}

// PRINTS
// Testy got: array(1) {
//   [0]=> int64(777)
// }
// Testy got: array(3) {
//   [0]=> int64(65)
//   [1]=> int64(66)
//   [2]=> int64(67)
// }
// Testy got: array(3) {
//   [0]=> int64(10)
//   [1]=> int64(20)
//   [2]=> int64(30)
// }
// Testy got: array(3) {
//   [0]=> int64(10)
//   [1]=> int64(20)
//   [2]=> int64(30)
// }
// Testy got: array(3) {
//   [0]=> int64(10)
//   [1]=> int64(20)
//   [2]=> int64(30)
// }

The Testy function demonstrates the behavior of the int64_list_t argument type, which is quite versatile and can handle various input types. Even when a single value is passed, it is automatically converted into an array with one element. This allows for flexibility in function calls, accommodating both single values and arrays of int64 types.

An argument that requires Arrays of Strings:

printr Testy(777);
printr Testy(array('A', 'B', 'C'));
printr Testy(array("10", "20", "30"));
printr Testy(array(10, 20, 30));
printr Testy(array(10.0, 20.0, 30.0));

Function Testy(string_list_t $p)
{
    print "Testy got: " . vardump($p)->>TrimEnd();
}

// PRINTS
// Testy got: array(1) {
//   [0]=> string(3) "777"
// }
// Testy got: array(3) {
//   [0]=> string(1) "A"
//   [1]=> string(1) "B"
//   [2]=> string(1) "C"
// }
// Testy got: array(3) {
//   [0]=> string(2) "10"
//   [1]=> string(2) "20"
//   [2]=> string(2) "30"
// }
// Testy got: array(3) {
//   [0]=> string(2) "10"
//   [1]=> string(2) "20"
//   [2]=> string(2) "30"
// }
// Testy got: array(3) {
//   [0]=> string(2) "10"
//   [1]=> string(2) "20"
//   [2]=> string(2) "30"
// }

The Testy function demonstrates the behavior of the string_list_t argument type, which accepts arrays of strings as input. It automatically converts single values into arrays with one element, ensuring consistency in function calls. The function can handle arrays of strings of any length, providing flexibility for working with string data.

A very basic example of a custom TypeDef as an argument:

TypeDef(q(unsigned intptr <>mytype));

printr Testy(array(10, 20, 30));

Function Testy(mytype $p)
{
    print "Testy got: " . vardump($p)->>TrimEnd();
}

// PRINTS
// Testy got: array(3) {
//   [0]=> uintptr(10)
//   [1]=> uintptr(20)
//   [2]=> uintptr(30)
// }

In this example, we define a custom typedef mytype using the TypeDef function. The typedef is defined as unsigned intptr, indicating that it represents unsigned integer values stored as pointers.

The Testy function takes an argument of type mytype, and when called with an array of integers, it automatically converts the input into an array of mytype elements. The function then prints the converted array, displaying the values as uintptr types.

This demonstrates how custom typedefs can be used to create aliases for specific data types, providing clarity and flexibility in function signatures and variable declarations.

Using a Struct as a parameter and passing an associative array:

TypeDef(q~
    struct {
        int x;
        int y;
    } POINT;
~);

my $p = array("x" => 100, "y" => 200);
printr CalcPoint($p);

Function CalcPoint(POINT $p)
{
    my $x = VecEx($p, TypeDefMember("POINT", "x"));
    my $y = VecEx($p, TypeDefMember("POINT", "y"));
    say "X=$x, Y=$y";
    say "Binary=" . $p[<>];
}

// PRINTS
// X=100, Y=200
// Binary=64 00 00 00 C8 00 00 00 -- -- -- -- -- -- -- -- d...È...

In the example, an associative array is provided as the argument when calling the CalcPoint function. The array contains "x" => 100 and "y" => 200 as the key-value pairs, representing the coordinates.

Upon invoking the function, Sputnik automatically converts the associative array into the POINT struct format. Inside the function, the individual members x and y can be accessed using the VecEx function and TypeDefMember function.

The function then demonstrates the conversion by printing the values of x and y, as well as the binary representation of the POINT struct.

This showcases how typedefs can be used to define custom data structures and easily convert data to and from those structures within function arguments.

Using a Struct as a parameter and passing a list array:

TypeDef(q~
    struct {
        int x;
        int y;
    } POINT;
~);

my $p = array(100, 200);
printr CalcPoint($p);

Function CalcPoint(POINT $p)
{
    my $x = VecEx($p, TypeDefMember("POINT", "x"));
    my $y = VecEx($p, TypeDefMember("POINT", "y"));
    say "X=$x, Y=$y";
    say "Binary=" . $p[<>];
}

// PRINTS
// X=100, Y=200
// Binary=64 00 00 00 C8 00 00 00 -- -- -- -- -- -- -- -- d...È...

In this example, we showcase the parameter type conversion feature of Sputnik using the CalcPoint function.

The CalcPoint function takes a POINT struct as its parameter. The POINT struct has two members: x and y, which are integers representing coordinates.

In the example, an indexed array is provided as the argument when calling the CalcPoint function. The array contains the values [100, 200], representing the x and y coordinates, respectively.

Sputnik automatically converts the indexed array into the POINT struct format based on the sequence of elements. Inside the function, the individual members x and y can be accessed using the VecEx function and the TypeDefMember function.

The function then demonstrates the conversion by printing the values of x and y, as well as the binary representation of the POINT struct.

This showcases how Sputnik allows the conversion from an indexed array to a struct based on the element sequence, enabling flexible data conversion options for function parameters.

Using a Struct as a parameter and passing a class:

Class Testy
{
    my $x;
    my $y;
    Function __Construct($x, $y)
    {
        @this->$x = $x;
        @this->$y = $y;
    }
}

TypeDef(q~
    struct {
        int x;
        int y;
    } POINT;
~);

my $p = new Testy(100, 200);
printr CalcPoint($p);

Function CalcPoint(POINT $p)
{
    my $x = VecEx($p, TypeDefMember("POINT", "x"));
    my $y = VecEx($p, TypeDefMember("POINT", "y"));
    say "X=$x, Y=$y";
    say "Binary=" . $p[<>];
}

// PRINTS
// X=100, Y=200
// Binary=64 00 00 00 C8 00 00 00 -- -- -- -- -- -- -- -- d...È...

In this example, we show how a class instance can be converted into a struct argument. The CalcPoint function takes an argument of type POINT, which is a struct with x and y members. By passing an instance of the Testy class to CalcPoint, Sputnik automatically converts the class variables into the struct format. This demonstrates the flexibility of Sputnik in converting different data types into the required struct format.

Using a Struct as a parameter but seeing what happens if Binary is passed:

TypeDef(q~
    struct {
        int x;
        int y;
    } POINT;
~);

my $p = Pack("ii", 77, 88);
printr CalcPoint($p);

my $p = TypeDefInst("POINT", 20, 10);
printr CalcPoint($p);

Function CalcPoint(POINT $p)
{
    my $x = VecEx($p, TypeDefMember("POINT", "x"));
    my $y = VecEx($p, TypeDefMember("POINT", "y"));
    say "X=$x, Y=$y";
    say "Binary=" . $p[<>];
}

// PRINTS
// X=77, Y=88
// Binary=4D 00 00 00 58 00 00 00 -- -- -- -- -- -- -- -- M...X...
// 
// X=20, Y=10
// Binary=14 00 00 00 0A 00 00 00 -- -- -- -- -- -- -- -- ........

To learn more see TypeDefs and Return Types.


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