簡體   English   中英

Pine Script v5 編譯器報告“未聲明的標識符”

[英]Pine Script v5 compiler report "undeclared identifier"

像這樣的代碼:

//@version=5
indicator("My script")

x = if open > close
    var a = 10
    var b = 20
    a := 20
    b := 30
    (a+b)[1]
plot(x)

(a+b)[1] 中的編譯器報告“a”和“b”是未聲明的標識符:

line 11: Undeclared identifier 'a';
line 11: Undeclared identifier 'b'

但是如果將 expr '(a+b)[1]' 修改為 'a+b',則在塊中聲明了 'a' 和 'b',編譯成功。 編譯器是如何工作的?

就我而言,編譯器在第 11 行(在else分支中)專門用a報告了這個問題。 在這種情況下,會出現錯誤,因為a變量是在if分支中聲明的,而在else分支中根本不存在。

為此,您需要在if/else條件之外聲明。 這是重寫此代碼的一種可能方法:

//@version=5
indicator("My script")

f() =>
    var a = 10
    var b = 20
    if open > close
        a := 20
        b := 30
        (a+b)[1]
    else
        a
x = f()
    
plot(x)

很多時候,編譯器消息與問題不完全匹配,您必須猜測它試圖說什么。

在這種情況下,從邏輯上講,聲明不應在“if”部分中,因為在腳本中的任何地方都可以訪問變量,因此您必須在最低級別聲明它們。

You can use it like (a[1]+b[1]) right in the meantime

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM