Xalladus
Xalladus

Reputation: 13

Pine Script, function with a switch not recognizing the switch condition

In Pinescript I have created arrays that contain lines, labels, and boxes. To clear the data from the arrays I created a small function where the first argument dictates what kind of array it is going to clear. The 2 errors that I get states that I cannot use "line" on the label and box functions. This tells me that the switch isn't working, but I can't figure out why.

// Function to delete 3 different kinds of arrays
f_emptyArr(_type, _arr) =>
    for x = 0 to array.size(_arr) -1
        switch _type
            "line" => line.delete(array.get(_arr, x))
            "label" => label.delete(array.get(_arr, x))
            "box" => box.delete(array.get(_arr, x))

// Call to this function results in an error
f_emptyArr("line", lineArray)

I tried changing this to an if statement as well and got the same errors. I am still learning Pinescript, but I couldn't find anything about this anywhere. Help is appreciated.

Upvotes: 1

Views: 302

Answers (1)

vitruvius
vitruvius

Reputation: 21149

Even though you have a switch statement, the compiler must still check the data type.

As a workaround, you can overload functions.

f_emptyArr(line[] _arr) =>
    for x = 0 to array.size(_arr) -1
        line.delete(array.get(_arr, x))

f_emptyArr(label[] _arr) =>
    for x = 0 to array.size(_arr) -1
        label.delete(array.get(_arr, x))

f_emptyArr(box[] _arr) =>
    for x = 0 to array.size(_arr) -1
        box.delete(array.get(_arr, x))

lineArray = array.new_line()

// Call to this function results in an error
f_emptyArr(lineArray)

Upvotes: 1

Related Questions