ref argument picks up previous function argument on name clash with calling context
-
Sample program shared with ID NXP87NKD51 (pending)
A bunch of conditions to this one :) If you have a function with several arguments, an early non-ref argument and a later ref argument, and you call that function and give a variable with the same name as the non-ref argument to the ref slot, the function will receive the non-ref argument value in both arguments.
Sample program in text form, shortened:
function test(a, ref b) print("a = ", a, "\n") print("b = ", b, "\n") return void function main() x = "x" a = "a" test(x, a) return void main()
would be expected to print
a = x b = a
but prints
a = x b = x
If you make a a ref argument in the function definition, too, all is fine. If you make b a non-ref argument, all is fine. If you swap the order of the arguments, all is fine. If you rename a in the calling function, all is fine.
If you modify the ref argument, thankfully, the variable passed in at the non-ref argument outside the function stays unaffected. Inside, yeah, changing b also changes a.
Reliable workaround (seems so): always put the ref arguments first.
I'm not overly bothered, more intrigued, espeically how making the first argument ref too fixes it. -
That is indeed a weird and interesting bug.
I've edited my post to remove my observations with this, because it turns out you had already found out about them, I just didn't notice them in your post :)
-
This is being looked and currently and will most likely be fixed in the next patch!