HSL Static Vector Initialization

I am currently trying to create a static vector that will be used to convert certain values based on the indices. The goal is to define and initialize this vector in a single line or a double line, as this approach would not only be more memory-efficient but also streamline the program’s flow.

The challenge is to avoid defining an array of set values one by one each time the submethod is called, enabling the static elements to be referenced without the need to build and destroy an array repeatedly.

Here are the approaches I’ve tried so far:

Approach 1: Direct Initialization During Declaration

namespace _Method {
    method main() void {
        long vector[3] = {1, 3, 5};
    }
}

Error: Syntax errors related to array identifier and missing semicolons.

Approach 2: Using a Hypothetical set Function

namespace _Method {
    method main() void {
        long vector[3];
        set(vector, {1, 3, 5});
    }
}

Error: set is an undeclared identifier.

Approach 3: Inline Assignment with a create Function

namespace _Method {
    function create(long[] values) long[] {
        long arr[3];
        for (int i = 0; i < 3; i++) {
            arr[i] = values[i];
        }
        return arr;
    }

    method main() void {
        long vector[3] = create({1, 3, 5});
    }
}

Error: Various syntax errors, including undeclared identifiers and missing symbols.

Approach 4: Using Braces Directly

namespace _Method {
    method main() void {
        long vector[] = {1, 3, 5};
    }
}

Error: Similar syntax errors related to array declaration and initialization.

Approach 5: Loop Initialization (Fallback)

namespace _Method {
    method main() void {
        long vector[3];
        long values[3] = {1, 3, 5};
        for (int i = 0; i < 3; i++) {
            vector[i] = values[i];
        }
    }
}

While this approach works, it defeats the purpose of achieving a concise one-line or two-line initialization.

I am seeking advice on whether a one-line or two-line vector initialization is possible in HSL with a different syntax or method. Any suggestions or insights would be greatly appreciated.

Thank you!

Does HSL have vectors? I thought HSL was more similar to C# than C++?

Maybe I’m interpreting vector wrong here (in c++ a vector is a dynamic array) but in HSL that’s just a standard array?

Edit: I’m surprised any of these compiled in the HSL editor, I couldn’t get your fallback to compile either?

Edit2: Oh I see a vector means something different here, seems kinda like an overloaded term

1 Like

I think HSL is similar to C instead of C++/C#. For the array, I do not think there is initialization for it. But I think we can do it by multiple functions, like other functions (StrConcat?, Invoke?, Trace?). For hsl do not support variable number of arguments and method overloading, maybe this is not a bad way.

function Set1(variable a[], variable value1) void{
   a.SetSize(1);
   a[0]=value1;
}
function Set2(variable a[], variable value1,variable value2) void{
   a.SetSize(2);
   a[0]=value1;
   a[1]=value2;
}
function Set3(variable a[], variable value1,variable value2,variable value3) void{
   a.SetSize(3);
   a[0]=value1;
   a[1]=value2;
   a[2]=value3;
}
method main()
{
   variable b[];
   Set3(b, 1, 2, 3);
   Trace(b[1]);
   return;
}

2 Likes

So, unfortunately, all I’ve been able to work out is using a whole bunch of AddAsLast function calls.

Ideally, I would have preferred to define the contents of an array/vector/dictionary in a single, distinct call, especially if it is of a fixed size.

I also noticed that a variable must first be initialized before being assigned a value. This contrasts with C#/C++/C implementations, where assignment can occur at initialization. Unless I’m missing something significant regarding these assignments, the only solution I see involves multiple lines and function calls to define a variable or array.

Below is an example of the vector I am constructing, which I wish could be defined much more succinctly:

t_arrFltPrefixFactors.SetSize(0);
t_arrFltPrefixFactors.AddAsLast("0.000000000000000000000001");  // yocto
t_arrFltPrefixFactors.AddAsLast("0.000000000000000000001");     // zepto
t_arrFltPrefixFactors.AddAsLast("0.000000000000000001");        // atto
t_arrFltPrefixFactors.AddAsLast("0.000000000000001");           // femto
t_arrFltPrefixFactors.AddAsLast("0.000000001");                 // pico
t_arrFltPrefixFactors.AddAsLast("0.000001");                    // micro
t_arrFltPrefixFactors.AddAsLast("0.001");                       // milli
t_arrFltPrefixFactors.AddAsLast("0.01");                        // centi
t_arrFltPrefixFactors.AddAsLast("0.1");                         // deci
t_arrFltPrefixFactors.AddAsLast("1");                           // base
t_arrFltPrefixFactors.AddAsLast("10");                          // deca
t_arrFltPrefixFactors.AddAsLast("100");                         // hecto
t_arrFltPrefixFactors.AddAsLast("1000");                        // kilo
t_arrFltPrefixFactors.AddAsLast("1000000");                     // mega
t_arrFltPrefixFactors.AddAsLast("1000000000");                  // giga
t_arrFltPrefixFactors.AddAsLast("1000000000000");               // tera
t_arrFltPrefixFactors.AddAsLast("1000000000000000");            // peta
t_arrFltPrefixFactors.AddAsLast("1000000000000000000");         // exa
t_arrFltPrefixFactors.AddAsLast("1000000000000000000000");      // zetta
t_arrFltPrefixFactors.AddAsLast("1000000000000000000000000");   // yotta

@EricSindelar_Hamilton or @BrandonBare_Hamilton do you know if it’s possible to define an array or vector’s values without using a whole bunch of .AddAsLast functions?

I was able to use a VB object to run VB code which successfully passed in a predefined array but ideally I’d like to just be able to define an array with known values without a whole bunch of function calls or the need to call external objects to define them for me!

Hi @zachary.milot ,

The functions that you listed above is the intended way to initialize an array with values. The alternative approach would be to specify the array to be a specific size and to set each value to be at a specific index. This being said, it would require the same number of lines within the hsl, but the order would not matter.

1 Like