【发布时间】:2022-08-08 23:06:42
【问题描述】:
我遇到了一个问题,我似乎在 Tradingview 的 pine 脚本 v5 中发现了一个关于重载功能的错误。
代码示例:
//@version=5
indicator(\"Overload recognition from within custom functions not working consistently\")
mult(array<float> x1, string x2) =>
[x1, x2]
mult(array<string> x1, string x2) =>
array.push(x1, x2)
x1
mult(bool x1, string x2) =>
[x1, x2]
myFn(y1) => // workaround: must define parameter type for the overload to work successfully
mult(y1, \"Overload NOT recognized (unless used previously or type is defined explicitly before function parameter\")
var stringArray = array.new_string(0,na)
// mult(stringArray, \"Overload working as expected\")
// mult(true, \"Overload working as expected\")
myFn(stringArray)
plot(1, \"Overload recognition from within custom functions not working consistently\")
上面的代码编译会抛出错误:
Add to Chart operation failed, reason: line 16: Cannot call \'mult\' with argument \'x1\'=\'y1\'. An argument of \'string[]\' type was used but a \'float[]\' is expected问题是,如果我使用 y1 引用之前创建的字符串数组并从 myFn(y1) 中调用 mult(y1, \"...\") 函数,编译器无法识别我的参数的引用类型并识别相应的函数重载,响应第一场比赛\"你可能想要 float[],顺便说一句,这是错误的\".似乎有一个参考错误,并且松树脚本中有一个烦人的错误。
到目前为止可能的解决方法:
- 如果我为我的函数
myFn(array<string> y1) =>提供类型定义,而实际上我不想这样做,则找到正确的重载 - 如果我取消注释以下行:
mult(stringArray, \"Overload working as expected\")在我的函数调用之前,触发了一些与我相应的重载有关的重载的内部逻辑,重载也将在我的 fn 调用中被发现 - 实际上甚至不是解决方法......而只是一个事实 为什么封装对我来说如此重要?我在图书馆工作。
现在有人有更好的主意吗?
标签: pine-script pinescript-v5 tradingview-api