"Sputnik" help  
Sputnik Help
Generic(<input>, <strict>: <strictExpression>, <type1>: <expression1>, <type2>: <expression2>, ..., <default>: <defaultExpression>)

Description

Performs a type-based evaluation on the input and returns the corresponding expression based on the matching type. If no matching type is found, the default expression is returned. The strict parameter determines whether the type checks are performed with strict or loose rules.

Parameters

<input> The input value to be evaluated.
<strict>
<strictExpression>
A boolean value indicating whether strict type checking is enabled (true) or disabled (false).
<type1> The type to match for the input.
<expression1> The expression to be returned if the input matches <type1>.

<type2> The type to match for the input.
<expression2> The expression to be returned if the input matches <type2>.
... Additional types and expressions to match (can be a of 1 or as many as desired)
<default>
<defaultExpression>
The default expression to be returned if no matching type is found.

Return Value

The evaluated expression based on the matching type or the default expression if no matching type is found.

Remarks

The Generic function allows for type-based evaluations, where the input value is checked against different types and the corresponding expression is returned based on the matching type. Each type-expression pair is defined using the <type>: <expression> syntax. The expressions can be any valid Sputnik code and may include variable substitutions using the $value placeholder.

By default, the type matching is performed using loose type checking. If the strict parameter is set to true, strict type checking will be enabled. In strict mode, an exact type match is required for the evaluation to occur. If no matching type is found and strict type checking is disabled, the default expression will be returned.

It is important to note that the evaluation is performed in order, and the first matching type encountered will be used. If no matching type is found and strict type checking is disabled, the default expression will be returned.

Related

Example

Basic example:

say generic(100.0, double: 1, short: 2, int: 3, float: 4); // 1
say generic((int)100, double: 1, short: 2, int: 3, float: 4); // 3
say generic(100f, double: 1, short: 2, int: 3, float: 4); // 4
say generic(10.0f, float:1, double:2, default:3); // 1
say generic(10.0f, float:1, double:2, default:3); // 1
say generic(10.0f, float:1, double:2); // 1
say generic(10.0, float:1, double:2, default:3); // 2
say generic(10.0, float:1, double:2); // 2
say generic("cat", float:1, double:2, default:3); // 3
say generic(100, double: 1, uinteger: 2, integer: 3, float: 3); // 3
say generic((int16)100, double: 1, uinteger: 2, integer: 3, float: 3); // 3
say generic((int32)100, double: 1, uinteger: 2, integer: 3, float: 3); // 3
say generic((int64)100, double: 1, uinteger: 2, integer: 3, float: 3); // 3
say generic(100u, double: 1, integer: 2, uinteger: 3, float: 3); // 3
say generic((uint16)100, double: 1, integer: 2, uinteger: 3, float: 3); // 3
say generic((uint32)100, double: 1, integer: 2, uinteger: 3, float: 3); // 3
say generic((uint64)100, double: 1, integer: 2, uinteger: 3, float: 3); // 3

// PRINTS
// 1
// 3
// 4
// 1
// 1
// 1
// 2
// 2
// 3
// 3
// 3
// 3
// 3
// 3
// 3
// 3
// 3

Example of using the $value in the expressions:

say generic(100, double: "Double $value", int64: "Int $value", default: "Unknown");
say generic(100.0, double: "Double $value", int64: "Int $value", default: "Unknown");
say generic("cat", double: "Double $value", int64: "Int $value", default: "Unknown");

// PRINTS
// Int 100
// Double 100
// Unknown

Another $value example:

say generic(100.0, double: 10 * $value, short: 2, int: 3, float: 4);

// PRINTS
// 1000

An example of matching multiple types from one name such as pointer and fp:

say generic((IntPtr)1, double: 1, uinteger: 2, integer: 3, float: 3, pointer: 4);
say generic((UIntPtr)2, double: 1, uinteger: 2, integer: 3, float: 3, pointer: 4);
say generic(1.0f, uinteger: 1, integer: 2, fp: 3);
say generic(2.0, uinteger: 1, integer: 2, fp: 3);
say generic((Float)1, uinteger: 1, integer: 2, fp: 3);
say generic((Double)2, uinteger: 1, integer: 2, fp: 3);

// PRINTS
// 4
// 4
// 3
// 3
// 3
// 3

Example of using code blocks:

my $str = "cat";
say generic($str, string: {{return "Str $str";}}, default: {{return "Unknown";}})();
say generic("dog", string: @{{return "Str $value";}}, default: @{{return "Unknown";}});
// Str cat
// Str dog

The top example has to access the string outside since it executes later when $value no longer exists. However the bottom one executes on the spot hense it can read the $value directly.

An example of using the want context system combined with generic:

my $a = array("testy");
my $b = "testy";
my $c = 100;
my $d = 100.0;
printr generic($a, array: array("Cat", "Dog"), string: "Hello world", int64: 777, double: 13.37);
printr generic($b, array: array("Cat", "Dog"), string: "Hello world", int64: 777, double: 13.37);
printr generic($c, array: array("Cat", "Dog"), string: "Hello world", int64: 777, double: 13.37);
printr generic($d, array: array("Cat", "Dog"), string: "Hello world", int64: 777, double: 13.37);

Function Cat()
{
    if (@WantArray)
        return array("Cat", "Dog");
    if (@WantString)
        return "Hello world";
    if (@WantInt64)
        return 777;
    if (@WantDouble)
        return 13.37;
    return "oh";
}

// PRINTS
// Array
// (
//     [0] => Cat
//     [1] => Dog
// )
// Hello world
// 777
// 13.37

The combination of the Want context and the Generic function in Sputnik allows for powerful and flexible type-based evaluations and behavior. By utilizing these features together, you can create code that adapts dynamically to different types and contexts, enhancing code reusability and maintainability.

In the provided example, the code demonstrates how the Want context is used within the Generic function to handle different types of input values. The Generic function evaluates the input value based on the provided type-expression pairs and returns the corresponding expression that matches the Want context.

The Function Cat() showcases the usage of the Want context within an if-else logic structure. Depending on the value of the Want context, the function returns different values, including an array, a string, an int64 value, or a double value. This dynamic behavior allows the code to adapt its output based on the desired type specified in the Want context.

The resulting output from the printr statements demonstrates the flexibility of combining the Want context and the Generic function. Each printr statement returns the expected output based on the provided input and the matching type-expression pairs defined within the Generic function.

This combination of features in Sputnik opens up a wide range of possibilities for creating flexible and type-aware code. It enables you to handle different data types and contexts seamlessly, making your code more adaptable and versatile.

By leveraging the Want context and the Generic function, you can achieve type-specific operations, handle different return values, and customize the behavior of your code based on the desired type. This capability enhances the overall functionality and effectiveness of your Sputnik programs, allowing for more efficient and expressive code.

Overall, the ability to combine the Want context and the Generic function in Sputnik showcases the language's versatility and provides a powerful toolset for type-based evaluations and dynamic behavior.


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