"Sputnik" help  
Sputnik Help
Rule Bind

Description

This demonstrates how to create a function that can extend primitive types (String, Array etc) to behave as if they were class objects.

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

Remarks

The Bind rule in Sputnik allows you to create functions that extend the behavior of primitive types, such as strings and arrays, as if they were class objects. Bind functions enhance the functionality of these types by adding custom operations and behaviors, providing a more convenient and object-oriented way to manipulate them.

In the provided code example, two bind functions are demonstrated: "Push" and "Len".

Push Bind Function:
The "Push" bind function is defined using the "b" flag. It allows you to add an element to an array in-place without creating duplicate copies. By calling the "Push" function on an array, you can directly append a new element to the array without modifying its structure or creating unnecessary copies.

Len Bind Function:
The "Len" bind function is defined twice with different flags. The first "Len" bind function returns the character count of a string when applied to a string using the "b" flag. The second "Len" bind function returns the number of elements in an array when applied to an array using the "b" and "^" flags. The "^" flag in the second "Len" function allows this code to be added to the original "Len" bind function, extending its behavior to handle arrays as well.

The code example showcases the usage of these bind functions. An array is created, and the "Push" function is used to add a new element to the array. The array is then printed using "printr". The "Len" function is used to determine the length of a string and an array. The length of the string "Kitten" is printed using the "Len" function, followed by the length of an array [10, 20, 30, 40, 50]. The output demonstrates the extended functionality provided by the bind functions.

It's important to note that bind functions should be defined carefully to avoid conflicts with existing functions or unintended side effects. They are a powerful feature that allows you to extend the behavior of primitive types and make your code more expressive and intuitive.

Related

User Functions

Example

Code Example: Manipulating Arrays and Strings using Bind Functions

Description:
This code example demonstrates the usage of bind functions to manipulate arrays and strings in a convenient and object-oriented way. Bind functions allow you to extend the functionality of built-in types, such as arrays and strings, by adding custom operations and behaviors.

Usage:
The provided code showcases two main functionalities:

1. Modifying Arrays:
- The "Push" bind function is used to add an element to an array in-place without creating duplicate copies.
- By calling the "Push" function on an array, you can directly append a new element to the array without modifying its structure or creating unnecessary copies.

2. Operating on Strings and Arrays:
- The "Len" bind function is used to determine the length of a string or an array.
- When applied to a string, the "Len" function returns the character count of the string.
- When applied to an array, the "Len" function returns the number of elements in the array.
- By using the "Len" function, you can easily retrieve the length of strings or arrays without having to write separate code for each data type.

Note:
- Bind functions are designed to enhance the functionality of basic data types and provide a more intuitive and expressive way to manipulate them.
- The code example demonstrates the concept of bind functions and their potential applications for arrays and strings.
- Bind functions can be extended to other data types as well, depending on the specific needs and requirements of your application.
- Care should be taken when defining bind functions to ensure they do not conflict with existing functions or cause unintended side effects.

// create a bind called push which can be used on arrays
// to add an element to it
// the "b" causes it to become a bind function
"b" Function Push("&" $a, $b)
{
    if (IsVarArray($a))
        $a[] = $b;
}

// create a bind called len that can be used to
// get the length of a string
"b" Function Len($v)
{
    if (IsVarString($v))
        return StrLen($v);
}

// prefix to the original len bind function additional code
// that will return the length of an array
// the "^" causes this cause to get added to the original
"b,^" Function Len($v)
{
    if (IsVarArray($v))
        return Count($v);
}

// create an array
my $arr = array("Cat", "Dog", "Fox");
// use the bind push function
$arr->Push("Kitten");

// print the array
printr $arr;

// print a string len using the bind len functoin
say "Str " . "Kitten"->Len();

// print an array len using the bind len functoin
say "Array ". array(10, 20, 30, 40, 50)->Len();

// PRINTS
// Array
// (
//     [0] => Cat
//     [1] => Dog
//     [2] => Fox
//     [3] => Kitten
// )
// Str 6
// Array 5

This is how to create and use Bind functions.


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