K r = k(0, (S)(“upd”), ks((S)“test”), ki(42), (K)0);
My q upd function is:
upd:{[t;x] show " " sv string each (t;-16!x); };
That is, I send an integer to q via the callback and print test and reference count for x. The reference count in q is coming out as 3. This isn’t what I expected since the reference count starts at 0. I figured it would go to 1 in the upd call and then go back to 0 and get cleaned up.
There’s a lot of subtleties to the refcounting, your example falls somewhere in the middle of it. Here’s a couple of scenarios
/a constant is one ref count
q) { -16!42 } `
1i
/a local variable adds a ref count
q) { -16!x } 42
2i
/however using explicit apply adds another ref count (presumably because the parse tree adds another reference)
q) { -16!x }@42
3i
/having more than one input causes an additional ref count if using dot apply
q) { -16!y } [1i;42]
2i
q) { -16!y }.(1i;42)
3i
/but only if it’s a mixed list
q) { -16!y }.(1j;42)
2i
/again the parse trees tell a story
q) parse"{-16!y}[1i;42]"
{ -16!y }
1i
42
q) parse"{-16!y}.(1i;42)"
.
{ -16!y }
(enlist;1i;42)
q) parse"{-16!y}.(1j;42)"
.
{ -16!y }
(enlist;1;42)
/if you were to use value with list form, it would create another reference due to the formation of the mixed list
q) value ( { -16!y } ;1i;42)
4i
/drops back to 2 for a uniform list
q) value ( { -16!y } ;1j;42)
2i
/finally, back to your specific example - if you passed in two longs rather than the mixed list of “test” and a long, you should only see a ref count of 2