函数中 VAR 初始化的(非)持久性 [英] (non)persistance of VAR initializations in functions

查看:50
本文介绍了函数中 VAR 初始化的(非)持久性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我上一个问题的后续绘制自定义数据- 每天 = 正常,每周 = 不正常.
对已接受答案的评论表明,函数内 var 的状态在对该函数的连续调用中保持不变.
这被证明是正确的,因为删除 var 解决了我的问题.

This is a follow-up to my previous question Plotting custom data - daily = ok, weekly = not ok.
The comments on the accepted answer suggests that the state of a var inside a function is persisted throughout successive calls to that function.
This proved to be correct, because removing the var solved my problem.

然而,我现在有一个测试用例似乎证明了相反的情况.
在下面的脚本中,变量 b 似乎没有被持久化.

However, I now have a test case that seems to prove the opposite.
In the script below, variable b seems NOT to be persisted.

必须在每个柱上调用函数 f2() 以评估 if 语句.
情况就是这样,因为 y 的图等于条形的数量.

Function f2() has to be called on every bar in order to evaluate the if statement.
That is the case, since the plot of y is equal to the number of bars.

因为函数 f2() 也调用了 f1(),所以我希望 f1() 里面有变量 b> 每个条形也增加 1.
f1()b 的最终值在最后一个柱形上检索,并存储在 z 中.

Because function f2() also calls f1(), I expect variable b inside of f1() to also increase by 1 on each bar.
The final value of b inside of f1() is retrieved on the last bar, and stored in z.

令我惊讶的是,最后一个柱形后 z 的值显示为 1.
这意味着:

To my surprise, the value of z after the last bar showed to be 1.
This means that either:

  • var 变量不会在函数中持久化(被我的上一个问题)
  • 嵌套函数调用有自己的执行上下文.
  • var variables are not persisted within a function (disproved by my previous question)
  • nested function calls have their own execution context.

想不出造成这种行为的另一个原因.有人可以确认这一点,或者提供替代解释吗?

Can't think of another reason for this behaviour.
Can someone confirm this, or maybe provide an alternate explanation?

//@version=4
study("PlayGround")

var int y = 0
var int z = 0

f1() =>
    var int b = 0
    b := b + 1
    b

f2() =>
    f1()
    true

if true and f2()
    y := y + 1

if barstate.islast
    z := f1()

plot(y, title="y")
plot(z, title="z")

推荐答案

调用同一函数的不同实例,每个实例都维护自己的上下文,正如您所猜测的.因此,在您的情况下,从 f2() 中调用 f1() 维护的上下文与从 if barstate.islast块.

Different instances of a call to the same function each maintain their own context, as you surmised. So in your case, the call to f1() from within f2() maintains a different context than the one made from within the if barstate.islast block.

这既有优点也有缺点.它需要一个局部于函数作用域的持久变量(即,用 var 初始化)不能通过从脚本中的 2 个不同位置调用同一个函数来共享,但它允许在一个函数中重复使用一个函数上下文就像 3 个连续的 f_print() 调用.

This has both advantages and disadvantages. It entails a persistent variable (i.e., initialized with var) local to a function's scope cannot be shared by calling the same function from 2 different places in the script, but it allows repeated use of a function in a context like the 3 consecutive f_print() calls.

我们在这里讨论的是持久变量,但这个概念扩展到由函数调用创建的系列值,以及在局部变量上使用历史引用运算符时检索到的值.

We are discussing persistent vars here, but the concept extends to the series values created by function calls, and so the values retrieved when using the history-referencing operator on local variables.

这篇关于函数中 VAR 初始化的(非)持久性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆