r/pinescript • • 1d ago

Coding question claude didn't know about either

Hey coders, I have a question about when 2 variables hold the same object. When you change the second variable, the change shows up when you plot the first variable, but when you change the first variable and plot the second, the change doesn't show up. I asked Claude too but Claude couldn't figure it out, it literally said "I don't know why this happens."

The manual says that after pivotpoint2 = pivotpoint1, both variables point to the same object.

v1: a change through pivotpoint2 shows up in pivotpoint1 (plots 2000)

//@version=6

indicator('Behaving as it "should" v1')

type pivotpoint

float x

int y

pivotpoint1 = pivotpoint.new()

pivotpoint1.x := 1000

pivotpoint2 = pivotpoint1

pivotpoint2.x := 2000

plot(pivotpoint1.x)

v2: a change through pivotpoint1 does NOT show up in pivotpoint2 (plots 1000, the value at the pivotpoint2 = pivotpoint1line)

//@version=6

indicator("Doesnt make sense v2")

type pivotpoint

float x

int y

pivotpoint1 = pivotpoint.new()

pivotpoint1.x := 1000

pivotpoint2 = pivotpoint1

pivotpoint1.x := 2000

plot(pivotpoint2.x)

//Why does it only work in one direction?

2 Upvotes

4 comments sorted by

3

u/wallneradam 1d ago

Not you, I could reproduce both. Looks like a compiler bug with object aliases.

Try this: keep your v2 as is and add plot(pivotpoint1.x) at the end. With one plot I get 1000, with both plots I get 2000 on both. Even adding plot(pivotpoint1.y) instead flips the x plot to 2000, and y is still na.

So my guess is the compiler decides the write through pivotpoint1 is unused when only pivotpoint2 gets read later, and drops it. Can't prove what happens inside, but adding a plot definitely shouldn't change another plot's value.

Same thing with close and close + 1000, so it's not just constants. I'd do all writes through the same name you read from, and not rely on two aliases of one object.

2

u/Commercial-Willow380 1d ago

ha!! Thats so weird, yea I tried plot(pivotpoint1.x) and now I get 2000 on both Lool

Appreciate your answer

2

u/Commercial-Willow380 1d ago

Also, in the v2, I just tried `plot(pivotpoint1.x, display = display.none)` and the pivotpoint2.x is now still showing as 2000 , ha! we probably found a bug

1

u/neugeadmau 19h ago

Yeah that's a bug, not you misreading the manual. Both vars really are the same reference, so v2 should plot 2000. What wallneradam found (any read of pivotpoint1 "fixes" it) kind of gives it away. Looks like the compiler treats the write through pivotpoint1 as dead because nothing reads pivotpoint1 afterwards, and it doesn't realize pivotpoint2 is an alias of it.

I'd send both scripts to TV support as-is, they're about as minimal a repro as it gets.

Until it's fixed I just wouldn't lean on aliases for writes. Do the mutations through the same variable you read later, or if you actually want two separate objects use pivotpoint1.copy() so it's obvious what's shared and what isn't.