"Sputnik" help  
Sputnik Help
Language Reference - Datatypes - TypeDefs

TypeDefs

A TypeDef (see TypeDef Types) in Sputnik is a construct that allows you to create custom type definitions. It provides a way to define a new type based on existing types or a combination of existing types. Essentially, a TypeDef allows you to create an alias or a new name for an existing type.

By using TypeDefs, you can enhance code readability and expressiveness by providing meaningful names for specific data types or combinations of data types. It allows you to encapsulate the details of a particular data representation or structure under a single, easily recognizable name.

With TypeDefs, you can define complex data structures, create custom data types, or introduce abstractions in your code. They can be particularly useful when working with binary data or defining structures that require specific memory layouts.

In Sputnik, TypeDefs operate similarly to C's typedefs, providing the ability to create new types based on existing ones without the need for explicit type declarations. They offer flexibility and convenience when working with data types and contribute to the overall organization and maintainability of your code.

Typedefs in Sputnik provide flexibility and allow you to define custom data types for use in functions, structures, and other contexts. In the example you provided, the typedef "POINT" represents a structure with two integer members, "x" and "y".

Use use TypeDefs in function parameters read here.


Examples

Lists, character strings, byte strings and bit strings support various slicing and indexing possibilities through the [] operator.

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

Function GetPoint($x, $y) POINT
{
    return [$x, $y];
}

my $ret = GetPoint(200, 700);
say "X=" . VecEx($ret, TypeDefMember("POINT", "x"));
say "Y=" . VecEx($ret, TypeDefMember("POINT", "y"));
say $ret[<>];

// PRINTS
// X=200
// Y=700
// C8 00 00 00 BC 02 00 00 -- -- -- -- -- -- -- -- È.......

Similar to above but showing how to translate from arrays to structs:

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

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

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

// PRINTS
// CalcPoint got 100, 200
// 64 00 00 00 C8 00 00 00 -- -- -- -- -- -- -- -- d...È...

This example illustrates the use of a TypeDef to define a POINT struct with x and y fields in the Sputnik programming language. A CalcPoint function takes a POINT type as an argument and extracts the x and y values using TypeDefMember and VecEx functions.

The code then creates an array, $p, containing x and y values, and calls the CalcPoint function with this array. The CalcPoint function effectively translates the array to the POINT struct, and the extracted x and y values are printed using say.

CalcPoint got 100, 200
64 00 00 00 C8 00 00 00 -- -- -- -- -- -- -- --

This example demonstrates how Sputnik automatically translates the array elements to the corresponding struct fields when passed as an argument to a function expecting that specific type.

This time typedefs inside typedefs are demonstrated:

TypeDef(q~
    struct  {
        int width;
        int height;
    } SIZE;
~);

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

TypeDef(q~
    struct  {
        POINT loc;
        SIZE sz;
    } BOUNDS;
~);

my $a = TypeDefInst("BOUNDS", [100, 200], [32, 64]);


printr VecEx($a, TypeDefMember("BOUNDS", "loc"));

say $a[<>];

// PRINTS
// Array
// (
//     [x] => 100
//     [y] => 200
// )
// 64 00 00 00 C8 00 00 00 20 00 00 00 40 00 00 00 d...È... ...@...

This example demonstrates the use of TypeDef in the Sputnik programming language to create and manipulate structured data types. Three distinct types, namely SIZE, POINT, and BOUNDS, are defined using the TypeDef function.

The SIZE type consists of two integer members: width and height. Similarly, the POINT type has two integer members: x and y. The BOUNDS type combines the POINT and SIZE types as members.

An instance of the BOUNDS type, named a, is created using TypeDefInst. The values for the loc (POINT) and sz (SIZE) members are set to [100, 200] and [32, 64], respectively.

The printr function is used to print the values of the loc member of the BOUNDS instance a. The output showcases the array structure with x and y values.

Additionally, the binary representation of the BOUNDS instance is displayed using array indexing. The resulting binary data reflects the specified values for x, y, width, and height in little-endian format.

This example highlights the use of TypeDef to create custom data types with predefined structures. It showcases the ability to instantiate and manipulate instances of these types, providing a convenient way to work with complex data structures in Sputnik.

Here is a fairly intricate series of TypeDefs that constructs a final object:

TypeDef(q~
    struct  {
        int width;
        int height;
    } SIZE;
~);

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

TypeDef(q~
    struct  {
        POINT loc;
        SIZE sz;
    } BOUNDS;
~);

TypeDef(q~
    struct  {
        float x;
        float y;
    } POINTF;
~);

TypeDef(q~
    struct  {
        POINTF pos;
        BOUNDS bounds;
    } FB;
~);

my $a = TypeDefInst("FB", [20.5, 30.5], [[100, 200], [32, 64]]);

say "Accessing element: pos";
printr VecEx($a, TypeDefMember("FB", "pos"));

say "Accessing element: bounds.loc";
printr VecEx($a, TypeDefMember("FB", "bounds.loc"));

say "Accessing element: bounds.sz";
printr VecEx($a, TypeDefMember("FB", "bounds.sz"));

say "Accessing all elements";
printr (FB~~)$a;

say "Printing binary";
say $a[<>];

// PRINTS
// Accessing element: pos
// Array
// (
//     [x] => 20.5
//     [y] => 30.5
// )
// Accessing element: bounds.loc
// Array
// (
//     [x] => 100
//     [y] => 200
// )
// Accessing element: bounds.sz
// Array
// (
//     [width] => 32
//     [height] => 64
// )
// Accessing all elements
// Array
// (
//     [pos] => Array
//         (
//             [x] => 20.5
//             [y] => 30.5
//         )
//     [bounds] => Array
//         (
//             [loc] => Array
//                 (
//                     [x] => 100
//                     [y] => 200
//                 )
//             [sz] => Array
//                 (
//                     [width] => 32
//                     [height] => 64
//                 )
//         )
// )
// Printing binary
// 00 00 A4 41 00 00 F4 41 64 00 00 00 C8 00 00 00 ..¤A..ôAd...È...
// 20 00 00 00 40 00 00 00 -- -- -- -- -- -- -- --  ...@...

In this Sputnik script, the use of TypeDef allows for the creation and storage of custom data types, making it convenient to define complex structures by chaining these types together. The script starts by defining simple types like SIZE, POINT, BOUNDS, POINTF, and FB using the TypeDef construct. These types represent dimensions, coordinates, bounding boxes, floating-point coordinates, and a more complex structure combining these elements, respectively.

The script then instantiates an object of type FB using TypeDefInst. By providing arrays of values, the script initializes the various components of the FB object in a concise manner.

The subsequent VecEx and printr calls showcase the ease of accessing and printing the elements of the created object. The magic lies in the ability to chain these types together, creating a hierarchical structure without the need to redefine each type repeatedly. This feature enhances code readability and reduces redundancy.

Overall, the script demonstrates the power of using TypeDef in Sputnik to define and reuse custom data types, enabling the creation of intricate structures with simplicity and efficiency. This approach is especially useful when dealing with complex data models and hierarchies, making it easier to manage and manipulate structured data.

Here we demonstrate using a typedef to manage accounts:

#define PM(mem) TypeDefMember("Account", #mem)

TypeDef(q~
    struct {
        int32_t userId;
        int32_t userIcon;
        char_t userName[16];
        char_t login[16];
        char_t password[16];
        uint32_t privKick : 1;
        uint32_t privBan : 1;
        uint32_t privDownload : 1;
        uint32_t privUpload: 1;
    } Account;
~);

my $adminAcc = TypeDefInst("Account", 777, 777, "Administrator", "admin", "pass123", 1, 1, 1, 1);
my $guestAcc = TypeDefInst("Account", 1212, 1212, "Guest", "guest", "blank", 0, 0, 1, 1);

foreach(my $acc in [$adminAcc, $guestAcc])
{
    say "--- login " . VecEx($acc, PM(login)) . " ---";
    say "userId = " . VecEx($acc, PM(userId));
    say "userIcon = " . VecEx($acc, PM(userIcon));
    say "userName = " . VecEx($acc, PM(userName));
    say "password = " . VecEx($acc, PM(password));
    say "privKick = " . VecEx($acc, PM(privKick));
    say "privBan = " . VecEx($acc, PM(privBan));
    say "privDownload = " . VecEx($acc, PM(privDownload));
    say "privUpload = " . VecEx($acc, PM(privUpload));
    say;
}

// PRINTS
// --- login admin ---
// userId = 777
// userIcon = 777
// userName = Administrator
// password = pass123
// privKick = 1
// privBan = 1
// privDownload = 1
// privUpload = 1
// 
// --- login guest ---
// userId = 1212
// userIcon = 1212
// userName = Guest
// password = blank
// privKick = 0
// privBan = 0
// privDownload = 1
// privUpload = 1

This example showcases the use of the TypeDef function along with a macro, PM, to simplify struct member accessors in the Sputnik programming language. The defined Account type includes various fields such as userId, userIcon, userName, login, password, and several privilege flags.

Two instances of the Account type, adminAcc and guestAcc, are created using TypeDefInst. These instances represent an administrator and a guest account, each initialized with specific values for the fields.

A foreach loop iterates over an array containing both accounts, demonstrating the ease of accessing and printing individual struct members using the PM macro. The output includes information such as userId, userIcon, userName, password, and various privilege flags for each account.

This example demonstrates the use of macros to enhance code readability and simplify struct member access in Sputnik.

 


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